incorporate Paul's changes for query term flush and other cleanup