Refactored authentication code.