Added a Query class and refactored existing queries to use this object