Moved majority of state switching code into states.js

Also renamed current to global.