Fixes a number of bugs, and keeps the 2 versions in sync.
Copied from the accounts-dialog git module.