Refactoring of OAuth2 code (part 1)