fixed unsafe reduce function