refactored login code