Refactoring and login bug fix