extract out query code