Refactor retrieval of query object