implementing fsm.