Started major work on state machine.
Mostly finished state machine