Refactored login code