Initial state machine.
Various hacking.