fix potential concurrent modification of query queue; clean code