Added state machine logic.