Fixed the "forbidden redirect_uri" error and cleaned up the code.