   (vimgolf-clear-keystrokes)
   (setq vimgolf-prior-window-configuration (current-window-configuration)
         vimgolf-challenge challenge-id)
-  (let ((vimgolf-yaml-buffer (url-retrieve-synchronously (vimgolf-challenge-url challenge-id)))
-        (data-end-regexp "\\([ 	]\\{4\\}\\|[ 	]\\{0\\}\\)
-  type: [-a-z]+")
-        (data-start-regexp "  data: |\\+\\{0,1\\}
-"))
+  (let ((vimgolf-yaml-buffer (url-retrieve-synchronously (vimgolf-challenge-url challenge-id))))
     (set-buffer vimgolf-yaml-buffer)
     (beginning-of-buffer)
-    (unless (re-search-forward data-start-regexp nil t)
-      (error "Can't find data in response from vimgolf; check challenge ID."))
-    (vimgolf-kill-existing-session)
-    (let ((vimgolf-start-buffer (get-buffer-create vimgolf-start-buffer-name))
-          (vimgolf-work-buffer (get-buffer-create vimgolf-work-buffer-name))
-          (vimgolf-end-buffer (get-buffer-create vimgolf-end-buffer-name)))
-      (append-to-buffer vimgolf-start-buffer (point) (point-max))
-      (append-to-buffer vimgolf-work-buffer (point) (point-max))
-      (vimgolf-scrub-buffer vimgolf-start-buffer)
-      (vimgolf-scrub-buffer vimgolf-work-buffer)
-      (re-search-forward data-start-regexp nil t)
-      (append-to-buffer vimgolf-end-buffer (point) (point-max))
-      (vimgolf-scrub-buffer vimgolf-end-buffer)
-      (delete-other-windows)
-      (display-buffer vimgolf-end-buffer 'display-buffer-pop-up-window)
-      (set-window-buffer (selected-window) vimgolf-work-buffer)
-      (switch-to-buffer vimgolf-work-buffer)
-      (vimgolf-capture-keystrokes)
-      (setq vimgolf-working-window-configuration (current-window-configuration)))))
+    (let* ((start-text (vimgolf-read-next-data-chunk))
+           (end-text (vimgolf-read-next-data-chunk)))
+
+      (vimgolf-kill-existing-session)
+
+      (let ((vimgolf-start-buffer (get-buffer-create vimgolf-start-buffer-name))
+            (vimgolf-work-buffer (get-buffer-create vimgolf-work-buffer-name))
+            (vimgolf-end-buffer (get-buffer-create vimgolf-end-buffer-name)))
+
+        (vimgolf-init-buffer vimgolf-start-buffer start-text)
+        (vimgolf-init-buffer vimgolf-end-buffer end-text)
+        (vimgolf-reset-work-buffer)
+
+        ;; Set up windows
+        (delete-other-windows)
+        (display-buffer vimgolf-end-buffer 'display-buffer-pop-up-window)
+        (set-window-buffer (selected-window) vimgolf-work-buffer)
+        (switch-to-buffer vimgolf-work-buffer)
+        (setq vimgolf-working-window-configuration (current-window-configuration))
+
+        (vimgolf-capture-keystrokes)))))
 
 ;;; vimgolf.el ends here


;; (setq vimgolf-logging-enabled t)
;; (setq vimgolf-logging-enabled)
(defvar vimgolf-logging-enabled)

(defun vimgolf-log-keystroke ()
  (when (and vimgolf-logging-enabled (not (< 0 (recursion-depth))))

(defun vimgolf-challenge-url (challenge-id)
  (concat vimgolf-host (vimgolf-challenge-path challenge-id) vimgolf-challenge-extension))

(defun vimgolf-init-buffer (buffer text)
  (with-current-buffer buffer
    (erase-buffer)
    (insert text)
    (beginning-of-buffer)
    (vimgolf-mode t)))

(defun vimgolf-kill-existing-session ()
  (dolist (buf (list vimgolf-start-buffer-name
                     vimgolf-work-buffer-name
                     vimgolf-end-buffer-name
                     vimgolf-keystrokes-buffer-name
                     vimgolf-keystrokes-log-buffer-name))
    (when (get-buffer buf)
      (kill-buffer buf))))

(defun vimgolf-read-next-data-chunk ()
  "Return the next chunk of data as a string, leaving the point at the end of that chunk."
  (let ((data-start-regexp "  data: |\\+\\{0,1\\}
")
        (data-end-regexp "\\([ 	]\\{4\\}\\|[ 	]\\{0\\}\\)
  type: [-a-z]+"))
    (unless (re-search-forward data-start-regexp nil t)
      (error "Can't find data in response from vimgolf"))
    (let ((start (point)))
      (unless (re-search-forward data-end-regexp nil t)
        (error "Unclosed data section in response from vimgolf"))
      (let ((str (buffer-substring-no-properties start (match-beginning 0))))
        (replace-regexp-in-string "^    " "" str)))))

;;;###autoload
(defun vimgolf (challenge-id)
