combined map and reduce queries into one function