implementing reduce