Fixing the broken query