More on state machines.

Will make a short example to iron out bugs and demonstrate use.