Added a StateMachine and other structural changes