 
 (defn- syncspritemaptocharacters [spritemap characters]
   "Adds sprites for charaters that do not yet have them, and removes any sprites that do not have a character using them"
-  (let [characteruniqueids (set (map #(:uniqueid %1) characters))
+  (let [characteruniqueids (set (keys characters))
         spriteuniqueids (set (keys spritemap))
-        unusedsprites (difference spriteuniqueids characteruniqueids)
-        missingsprites (difference characteruniqueids spriteuniqueids)]
+        unusedspriteids (difference spriteuniqueids characteruniqueids)
+        missingspriteids (difference characteruniqueids spriteuniqueids)]
     ;; now add the new sprites and remove the old ones
-    ;;(js/console.log (clj->js missingsprites))
-    ;;(js/console.log (clj->js unusedsprites))
+    ;;(js/console.log (clj->js spritemap))
+    ;;(js/console.log (clj->js missingspriteids))
+    ;;(js/console.log (clj->js unusedspriteids))
     (-> spritemap
-        (generatemissingsprites missingsprites characters)
-        (removeunusedsprites unusedsprites))))
+        (generatemissingsprites (map characters missingspriteids))
+        (removeunusedsprites unusedspriteids))))
+
+(defn- syncsprites! [spritemapkeyword characters]
+  (let [
+        ;; if spritemap is nil, defaults to {}
+        spritemap (or (spritemapkeyword @displayed-renderstate) {})
+        syncedspritemap (syncspritemaptocharacters spritemap characters)]
+    ;; swap if something changed
+    (if (= spritemap syncedspritemap) nil 
+        (swap! displayed-renderstate assoc spritemapkeyword syncedspritemap))))
 
 (defn rebuildcharacterdisplaylist [gamestate]
-  (let [characters (:characters gamestate)
-        spritemap (:spritemap @displayed-renderstate)
-        syncedspritemap (syncspritemaptocharacters spritemap characters)]
+  (let [characters (:characters gamestate)]
     ;; mutating step to add missing sprites and remove unused sprites
-    (swap! displayed-renderstate assoc :spritemap syncedspritemap)
+    (syncsprites! :mapsprites characters)
     (let [renderstate @displayed-renderstate
           stage-characters (:stage-characters renderstate)]
       (.removeAllChildren stage-characters)
-      (doseq [character characters]
-        (displaycharactersprite renderstate character)))))
+      (doseq [character (vals characters)]
+        (displaycharactermapsprite renderstate character)))))
 
 ;;
 ;; map sprite function(s)

  (let [freshbitmap (createjs/BitmapAnimation. (getspritesheet "icons"))
        standardscale spritescale
        hightlightedscale (* 1.2 spritescale)]
    #_(js/console.log iconindex)
    ;; right now sprites are simple animations with one frame.
    (.gotoAndStop freshbitmap (:iconindex character))
    (makestandardsizeandorigin freshbitmap)

;; character sprite functions
;;

(defn- displaycharactermapsprite [renderstate character]
  (let [spritemap (:mapsprites renderstate)
        charactersprite (get spritemap (:uniqueid character))
        newx (* tilespacing (:x character))
        newy (* tilespacing (:y character))
        characterstage (:stage-characters renderstate)]
    ;;(js/console.log (clj->js spritemap))
    ;;(js/console.log (clj->js character))
    (positionsprite charactersprite newx newy)
    (makestandardsizeandorigin charactersprite)
    (.addChild characterstage charactersprite)))
