Modified the code to allow its use with generalised state machines.