Refactored login logic