Improvements to state machine