                (return (.apply (aget Math "max") nil vals))))
    :avg (js* (fn [ary field]
                (var vals (.map ary (fn [obj] (return (aget obj field)))))
-               (var len (aget ary "length"))
-               (if (= len 0)
-                 (return nil)
-                 (return (/ (.sum Array vals) len)))))
+               (return (.avg Array vals))))
    :median (js* (fn [ary field]
                   (var median (fn [array]
                                 (var array (.sort array))

                    (convert-keys name))
                {})
        agg-fields (->> aggregations
                        (map (comp second second))
                        (remove nil?))

        map-fn (generate-map-fn group agg-fields field-zip-fn)
        reduce-fn (generate-reduce-fn aggregations)]
    (array-map :mapReduce (name from)
               :map map-fn

     (generate-map-fn group-fields agg-fields identity))
  ([group-fields agg-fields zip-fn]
     (let [create-map-obj (fn [fields]
                            (reduce (fn [acc [out-field in-field]]
                                      (let [from-field (name (zip-fn in-field))]
                                        (merge acc {out-field (js* (aget this (clj from-field)))})))
                                    {} fields))
           map-id (create-map-obj group-fields)
           map-val (create-map-obj agg-fields)]
