             [io.pedestal.http.jetty.websockets :as ws]))
 
 (def ^:private game-rooms (atom {}))
-
 (def ^:private connections (atom {}))
+
+(defn- remove-chan-form-room
+  [game-id chan-id]
+  (swap! game-rooms update-in [game-id :players] dissoc chan-id))
+
+(defn- cleanup-closed-connections
+  []
+  (doseq [[chan-id {:keys [session]}] @connections]
+    (let [channel-open? (.isOpen session)]
+      (when-not channel-open?
+        ;; Remove channel from game rooms
+        (map (fn [game-id {players :players}]
+               (let [contains-connection? (chan-id players)]
+                 (when contains-connection?
+                   (remove-chan-form-room game-id chan-id))))
+             @game-rooms)
+        ;; Remove channel from connections
+        (swap! connections dissoc chan-id)))))
+
+(defn- cleanup-empty-games
+  []
+  (doseq [[game-id {:keys [players]}] @game-rooms]
+    (let [game-empty? (= (count players) 0)]
+      (when game-empty?
+        (swap! game-rooms dissoc game-id)))))
 
 (defn clean-connections
   "Removes all closed connections from state"
   [time]
-  (doseq [[chan-id {:keys [session]}] @connections]
-    (let [channel-open? (.isOpen session)]
-      (when-not channel-open?
-        (swap! connections dissoc chan-id)))))
+  (cleanup-closed-connections)
+  (cleanup-empty-games))
 
 (defn connection-clean-err
   "If the scheduler fails this will be called

             assoc-in
             [game-id :players chan-id]
             (assoc socket-user :color (:player/color player))))))

(defn- broadcast-game-message
  [game-id formatted-message]


  ;; Print connections
  (clojure.pprint/pprint @connections)

  (clojure.pprint/pprint @game-rooms)

  ;; Remove all closed connections
  (clean-connections 0)
