implementing reduce.