Login with some refactoring