Copied some code from accounts-rails