FSM implementation