Refactoring of querying