Refactoring Query class