made top-level FSM more general and flexible