refactored query to be DRYer, temp logging