changed reduce / mapreduce to loops