improved state machine