Generate the state machine, and wire the closure to call it