fix bugs in converting to state machine