refactoring and fix for login issues