                     wall :- s/Str
                     distance-maps :- [DistanceMap]
                     path-map :- (s/maybe PathMap)]
-  (let [changed-cells (or (when-not optimize-drawing (g/grid-coords grid))
+  (let [changed-cells (or (when-not optimize-drawing (set (g/grid-coords grid)))
                           (when path-map (:changed-cells (:distances path-map)))
                           (first (map (comp :changed-cells :distances) distance-maps))
                           (:changed-cells grid)
-                          (g/grid-coords grid))]
+                          (set (g/grid-coords grid)))]
     (draw-cell-backgrounds g grid cell-size background distance-maps changed-cells)
     (draw-cell-walls g grid cell-size wall changed-cells)
     (when path-map (draw-path g cell-size path-map changed-cells))

;;
;; Any variable called 'g' is a CanvasRenderingContext2D object.

(def optimize-drawing false)

(defn fill-rect [g style x y w h]
  (aset g "fillStyle" style)

  {:distances g/Distances
   :color-family s/Keyword})

(def strokewidth 1.0)

(s/defn draw-cell-backgrounds [g
                               grid :- g/Grid
                               cell-size :- g/NonNegativeInt
                               background :- s/Str
                               distance-maps :- [DistanceMap]
                               changed-cells]
  (doseq [coord changed-cells]
    (let [{:keys [distances color-family max-distance] :as distance-map} (first (filter #(contains? (:distances %) coord) distance-maps))
          [y x] (map #(* % cell-size) coord)
          cell (g/grid-cell grid coord)
          x (if (g/linked? cell (:west cell)) (- x 0.5) x)
          y (if (g/linked? cell (:north cell)) (- y 0.5) y)
          w (if (g/linked? cell (:east cell)) (+ cell-size 0.5) cell-size)
          h (if (g/linked? cell (:south cell)) (+ cell-size 0.5) cell-size)
          color (if distance-map
                  (util/make-color max-distance (distances coord) color-family)
                  background)]
