Added the FSM logic