refactoring login function