Fix query code bug