Cleaned up the state machine.