Support for merging queries, this should really all be refactored.