Fixes bug in application of reduce function