Fix bug with state machine