                    (bounds (cons (copy-marker (car bounds))
                                  (copy-marker (cdr bounds)))))
               (acons 'bounds bounds err))))
-         (err-overlay
-          (lambda (err)
-            (let* ((bounds (cdr (assoc 'bounds err)))
-                   (overlay (make-overlay (car bounds) (cdr bounds))))
-	      (push #'merlin--kill-error-if-edited
-		    (overlay-get overlay 'modification-hooks))
-              (if (merlin-error-warning-p (cdr (assoc 'message err)))
-                  (merlin-put-margin-overlay overlay
-                                             merlin-margin-warning-string
-                                             'merlin-compilation-warning-face)
-                (merlin-put-margin-overlay overlay
-                                           merlin-margin-error-string
-                                           'merlin-compilation-error-face))
-              overlay)))
          (errors   (mapcar err-point errors)))
     (setq merlin-pending-errors errors)
-    (when merlin-error-in-margin (setq merlin-pending-errors-overlays
-                                       (mapcar err-overlay errors)))))
+    (when merlin-error-in-margin
+      (dolist (err errors)
+        (let* ((bounds (cdr (assoc 'bounds err)))
+               (overlay (make-overlay (car bounds) (cdr bounds))))
+          (overlay-put overlay 'merlin-kind 'error)
+          (overlay-put overlay 'merlin-pending-error err)
+          (push #'merlin--kill-error-if-edited
+                (overlay-get overlay 'modification-hooks))
+          (if (merlin-error-warning-p (cdr (assoc 'message err)))
+            (merlin-put-margin-overlay overlay
+                                       merlin-margin-warning-string
+                                       'merlin-compilation-warning-face)
+            (merlin-put-margin-overlay overlay
+                                       merlin-margin-error-string
+                                       'merlin-compilation-error-face))
+          overlay)))))
 
 (defun merlin--error-check (view-errors-p)
   "Check for errors.

				     end
				     &optional length)
  "Remove an error from the pending error lists if it is edited by the user."
  (when is-after
    (let ((err (nth (position overlay merlin-pending-errors-overlays)
		    merlin-pending-errors)))
      (setq merlin-pending-errors
	    (delete err merlin-pending-errors))
      (setq merlin-pending-errors-overlays
	    (delete overlay merlin-pending-errors-overlays))
      (delete-overlay overlay))))

(defun merlin-error-display-in-margin (errors)
  "Given a list of ERRORS, put annotations in the margin corresponding to them."

  (if merlin-pending-errors
      (let ((err (pop merlin-pending-errors)))
        (goto-char (cadr (assoc 'bounds err)))
        (if merlin-pending-errors
            (message "%s (%d more errors, use %s to go to the next)"
                     (cdr (assoc 'message err))
