updated map/reduce logic