Refactored authentication process