Refactoring authentication