Refactor Query class to use a single function for query composition