Improved the finite state machine.

Not entirely sure how this will come together in the end.