 
 (defn tick-game
   "Tick the game by miliseconds of time"
-  [time]
+  []
   ;; Update the global entities state atom 
   (swap! entities #(map update-entity %)))
 
+;; TODO combine the game-loop and render-loop as systems that
+;; run synchronously
+
 (defn game-loop
   "Calculate changes based on the time since last change"
-  []
+  [renderer stage]
   ;; TODO Calculate the changes since the last game tick
-  ;; TODO should this be async? Allows skipping frames
-  (.setInterval js/window #(tick-game time) (/ 1000 60)))
+  (when-not @paused?
+    (tick-game)
+    (draw renderer stage)))
 
-(def stage (js/PIXI.Stage. 0x66ff99))
+(defn set-interval
+  "Creates an interval loop of n calls to function f per second.
+   Returns a function to cancel the interval."
+  [f n]
+  (.setInterval js/window f (/ 1000 n))
+  #(.clearInterval f (/ 1000 n)))
 
-(defn render-loop
-  "Renders the game every n seconds"
+(defn init-game
+  "Renders the game every n seconds.
+   Hashmap of game properties."
   []
   (let [;; TODO get the screen height and width
         ;; TODO allow the screen height and width to be variable?
         width 800
         height 600
-        renderer (js/PIXI.CanvasRenderer. width height)]
+        frame-rate 60
+        stage (js/PIXI.Stage. 0x66ff99)
+        renderer (js/PIXI.CanvasRenderer. width height)
+        main-loop (set-interval #(game-loop renderer stage) 60)]
     (dom/append! (sel1 :body) (.-view renderer))
-    (.setInterval js/window #(draw renderer stage) (/ 1000 60))))
+    ;; TODO return a function that also cleans up the canvas 
+    {:renderer renderer
+     :stage stage
+     :end main-loop}))
 
-(defn start []
+
+;; Start, stop, reset the game. Game is stored in an atom and
+;; referenced directly in these functions
+(defn start-game! []
   (create-entity! stage "static/images/bunny.png" 500 500 0.05 0.05)
-  (render-loop)
-  (game-loop))
+  (create-entity! stage "static/images/bunny.png" 400 400 0.05 0.05)
+  (create-entity! stage "static/images/bunny.png" 200 200 0.05 0.05)
+  (reset! game (init-game)))
+
+(defn stop-game!
+  "Stop the game loop and remove the canvas"
+  []
+  (when @game
+    (dom/remove! (sel1 :canvas))
+    (:end @game)))
+
+(defn reset-game!
+  "End the"
+  []
+  (stop-game!)
+  (reset! game (start-game!)))

   Renders the stage to the screen"
  [renderer stage]
  (info "draw function called")
  (when-not @paused
    (doseq [e @entities]
          (render-entity stage e))
    (.render renderer stage)))

  (:use-macros [dommy.macros :only [node sel sel1]])
  (:require-macros [cljs.core.async.macros :refer [go]]))


;; TODO need a defonce so we can eval more than once
;; Global game state
(def game (atom nil))
(def systems (atom []))
(def entities (atom []))
(def paused? (atom false))

;; TODO make a record and protocol for an entity
(defn create-entity!
