Extract StateMachine module from Nessy

There are probably some drawbacks to this, but it seems generally good to try and extract this module, even if we don't upload it to CPAN.