Fixed bug in state machine