OAuth refactoring and bugfix