   (println @temp))
 
 (defn start-repl-thread []
+  (on ::repl-exit #((event ::repl-write :text "(println \"exit hack\")")))
   (future (try
            (capture-out-write
             #(event ::repl-print :text %)
             (let [read-q (LinkedBlockingQueue.)
-                  exit (ref false)]
+                  exit (atom false)]
               
               (on ::repl-write #(.put read-q (PushbackReader. (StringReader. (:text %)))))
-              (on ::repl-exit #((dosync (ref-set exit true))
-                                (Thread/sleep 300)
-                                (event ::repl-write :text "(println \"exit hack\")")))
-
+              (on ::repl-exit #((compare-and-set! exit false true)))
+              
               (on ::repl-ping #((if (= @exit false) (event ::repl-pong))))
               
               (clojure.main/repl

  (start-repl-thread))

(comment

  (start-repl-thread)

  (on ::repl-exception #(println (str "exception: " (:text %))))

  (on ::repl-ready #(println "repl ready"))
  (event ::repl-exit)

  (event ::repl-write :text "(println \"hello world\")")) 

;; GUI part

