fixed reduce function