state machine seems to work