fix reduce function