Optimize the state machine