 (setq floobits-open-buffers nil)
 (setq floobits-follow-mode nil)
 (setq floobits-user-highlights (make-hash-table :test 'equal))
-(setq floobits-python-path (concat (file-name-directory load-file-name) "floobits.py"))
-
-(add-hook 'kill-emacs-hook 'floobits-kill-emacs-hook nil nil)
-
-(defun floobits-kill-emacs-hook ()
-  (floobits-destroy-connection)
-  (when (and (boundp 'floobits-python-agent) (process-live-p floobits-python-agent))
-    (kill-process floobits-python-agent)))
+(setq floobits-python-path (concat floobits-plugin-dir "floobits.py"))
 
 (defun floobits-add-hooks ()
   (add-hook 'after-change-functions 'floobits-after-change nil nil)
-  (add-hook 'post-command-hook 'floobits-buffer-list-change nil nil))
+  (add-hook 'post-command-hook 'floobits-post-command-func nil nil))
 
 (defun floobits-remove-hooks ()
   (remove-hook 'after-change-functions 'floobits-after-change)
-  (remove-hook 'post-command-hook 'floobits-buffer-list-change))
-
-(defun process-live-p (process)
-  "Returns non-nil if PROCESS is alive.
-  A process is considered alive if its status is `run', `open',
-  `listen', `connect' or `stop'."
-  (memq (process-status process)
-    '(run open listen connect stop)))
-
-(add-hook 'kill-emacs-hook 'floobits-kill-emacs-hook nil nil)
-
-(defun floobits-kill-emacs-hook ()
-  (floobits-destroy-connection)
-  (when (and (boundp 'floobits-python-agent) (process-live-p floobits-python-agent))
-    (kill-process floobits-python-agent)))
-
-(defun floobits-add-hooks ()
-  (add-hook 'after-change-functions 'floobits-after-change nil nil)
-  (add-hook 'post-command-hook 'floobits-buffer-list-change nil nil))
-
-(defun floobits-remove-hooks ()
-  (remove-hook 'after-change-functions 'floobits-after-change)
-  (remove-hook 'post-command-hook 'floobits-buffer-list-change))
-
-(defun process-live-p (process)
+  (remove-hook 'post-command-hook 'floobits-post-command-func))
+
+(defun floobits-process-live-p (process)
   "Returns non-nil if PROCESS is alive.
   A process is considered alive if its status is `run', `open',
   `listen', `connect' or `stop'."

  (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-add-hooks)
  (floobits-auth))

(defun floobits-destroy-connection ()

    (delete-process floobits-conn)))

(defun floobits-send-to-agent (req event)
  (if (floobits-process-live-p floobits-conn)
    (progn
      (floo-set-item 'req 'name event)
      (floo-set-item 'req 'version floobits-agent-version)
      (process-send-string floobits-conn (concat (json-encode req) "\n")))
    (progn
      (message "connection to floobits died :(")
      (floobits-destroy-connection))))

(defun floobits-get-text (begin end)
  (buffer-substring-no-properties begin end))
