changed to a better reduce function