Fixing a query bug