Some work on building more complex queries and conditionals

DO NOT MERGE (not that you would, this code is awful at the moment) -- This is a preliminary work in progress