Fixed bugs in state machines