         (if (> (length floobits-agent-buffer) position) (+ 1 position) position)))
       (floobits-listener process ""))))
 
-(defun floobits-auth()
+(defun floobits-auth ()
   (let ((req (list
     (cons 'username floobits-username)
     (cons 'room floobits-room)
     (cons 'secret floobits-secret)
     (cons 'room_owner floobits-room-owner))))
-    (send-to-agent req 'auth)))
+    (floobits-send-to-agent req 'auth)))
 
-(defun create-connection()
-  (setq floo (open-network-stream "floobits" nil floobits-agent-host floobits-agent-port))
-  (set-process-coding-system floo 'utf-8 'utf-8)
-  (set-process-filter floo 'floobits-listener)
+(defun floobits-create-connection ()
+  (setq floobits-conn (open-network-stream "floobits" nil floobits-agent-host floobits-agent-port))
+  (set-process-coding-system floobits-conn 'utf-8 'utf-8)
+  (set-process-filter floobits-conn 'floobits-listener)
   (floobits-auth))
 
-(defun change-func (var begin end &optional &rest old_length)
-  "does stuff"
-  (setq contents (buffer-substring-no-properties begin end))
-  (set var
-       (mapcar 'symbol-value '(begin end contents old_length))))
+(defun floobits-destroy-connection ()
+  (when (and (boundp 'floobits-conn) floobits-conn)
+    (message "deleting floobits conn")
+    (setq floobits-conn nil)))
 
-(defun send-to-agent (req event)
+; (defun floobits-change-func (var begin end &optional &rest old_length)
+;   "does stuff"
+;   (setq contents (buffer-substring-no-properties begin end))
+;   (set var
+;     (mapcar 'symbol-value '(begin end contents old_length))))
+
+(defun floobits-send-to-agent (req event)
   (add-to-list 'req (cons 'version floobits-agent-version))
   (add-to-list 'req (cons 'name event))
-  (process-send-string floo (concat (json-encode req) "\n")))
+  (process-send-string floobits-conn (concat (json-encode req) "\n")))
 
-(defun get-text (begin end)
+(defun floobits-get-text (begin end)
   (buffer-substring-no-properties begin end))
 
-(defun before-change (begin end)
+(defun floobits-before-change (begin end)
   (if (eq (_floobits-is-buffer-public (current-buffer)) t)
     (let ((text (floobits-get-buffer-text (current-buffer))))
       (add-to-list 'floobits-change-set (cons 'before text)))))
 
-(defun after-change (begin end old_length)
+(defun floobits-after-change (begin end old_length)
   (if (eq (_floobits-is-buffer-public (current-buffer)) t)
      (let ((text (floobits-get-buffer-text (current-buffer))))
       (add-to-list 'floobits-change-set (cons 'after text))
       (add-to-list 'floobits-change-set (cons 'full_path (buffer-file-name (current-buffer))))
-      (send-to-agent floobits-change-set 'change)
+      (floobits-send-to-agent floobits-change-set 'change)
     (setq floobits-change-set))))
 
-(defun after-new-buffer ()
+(defun floobits-after-new-buffer ()
   (let ((req '("event" "new-buffer")))
-    (add-to-list 'req (cons 'text (get-text point-min point-max)))
+    (add-to-list 'req (cons 'text (floobits-get-text point-min point-max)))
     (add-to-list 'req (cons 'path (file-name-directory load-file-name)))
-    (send-to-agent req 'new-buffer)))
+    (floobits-send-to-agent req 'new-buffer)))
 
 ;;(add-hook 'before-change-functions 'before-change nil nil)
-(add-hook 'after-change-functions 'after-change nil nil)
-(add-hook 'find-file-hook 'after-new-buffer nil t)
-
-(create-connection)
+(add-hook 'after-change-functions 'floobits-after-change nil nil)
+(add-hook 'find-file-hook 'floobits-after-new-buffer nil t)

    (buffer-substring-no-properties (point-min) (point-max)))

(defun floobits-event-disconnect (req)
  (message "Disconnected: %s" (cdr (assoc "reason" req))))

(defun floobits-event-room_info (req)
  "does a thing")

(defun floobits-event-join (req))

(defun floobits-event-part (req))

(defun floobits-event-edit (req)
  (let* ((filename (cdr (assoc "full_path" req)))

   (func (concat "floobits-event-" event)))
    (funcall (read func) req)))

(defun floobits-listener (process response)
  (setq floobits-agent-buffer (concat floobits-agent-buffer response))
  (let ((position (search "\n" floobits-agent-buffer)))
    (when position
