minor refactoring of state machine