Added comments and refactored authorization code