Tidy up the state machine