FSM code from lecture