Refactoring: Broke authenticate() into a few smaller functions.