重构state machine