Fix bugs in StateMachine and StateEvent. Add tests for bug fixes.