map reduce logic