significant rework of mapreduce code (based on chat with kdj)