moved state-related code to state.js