replace ES5 reduce with custom implementation