Refactoring and fixed authorisation code grant flow