Fixed heap corruption bug in StateMachine. After changing the state within itself the remaining code referenced freed memory.