Finished base query code.