state machine switches states correctly