improve state machine