state machine logic