Moved the token code into its own module.
Removed unneded try/catch.
Changed the scope variable to be a member rather than a global.