Refactoring of login functions