Refactor out duplicate query