copying prev FSM code