Refactors duplicated query code into QueryHelpers module