state machine code