Refactored authentication and authorization