finished factoring out query management from ui_state.

untested