refactored the two queries to each run once