Fix reduce function