refactor mapreduce