Move all state handling to state.js