Refactored authorize() to be more reusable.