Some cleanup and move state operations to the state file