Fixing a ciritcal issue with query construction