Refactored authentication flow