Add general reduce function