Updated query code