          (type (propertize type 'face 'font-lock-doc-face)))
     (if (string= type "null") name (concat name " : " type))))
 
-(defun merlin-imenu-parse-outline-item (prefix item)
-  "Parse one item of the outline tree."
-  (let* ((line (cdr (assoc 'line (assoc 'start item))))
-         (col (cdr (assoc 'col (assoc 'start item))))
-         (name (cdr (assoc 'name item)))
-         (kind (cdr (assoc 'kind item)))
-         (type (cdr (assoc 'type item)))
-         (sub-trees (cdr (assoc 'children item)))
-         (entry (merlin-imenu-create-entry prefix name type kind line col))
-         (position (merlin-imenu-compute-position line col))
-         (marker (cons entry (set-marker (make-marker) position))))
-    (cond ((string= kind "Value")
-           (setq value-list (cons marker value-list)))
-          ((string= kind "Type")
-           (setq type-list (cons marker type-list)))
-          ((string= kind "Exn")
-           (setq exception-list (cons marker exception-list))))
-    (if (and (listp sub-trees) (not (null sub-trees)))
-        (merlin-imenu-parse-outline-tree (concat prefix entry ".") sub-trees))))
-
-(defun merlin-imenu-parse-outline-tree (prefix outline)
-  "Parse outline tree."
-  (when (not (null outline))
-    (merlin-imenu-parse-outline-item prefix (car outline))
-    (merlin-imenu-parse-outline-tree prefix (cdr outline))))
+(defun merlin-imenu-parse-outline (prefix outline)
+  (dolist (item outline)
+    (let* ((line (cdr (assoc 'line (assoc 'start item))))
+           (col (cdr (assoc 'col (assoc 'start item))))
+           (name (cdr (assoc 'name item)))
+           (kind (cdr (assoc 'kind item)))
+           (type (cdr (assoc 'type item)))
+           (sub-trees (cdr (assoc 'children item)))
+           (entry (merlin-imenu-create-entry prefix name type kind line col))
+           (position (merlin-imenu-compute-position line col))
+           (marker (cons entry (set-marker (make-marker) position))))
+      (cond ((string= kind "Value")
+             (setq value-list (cons marker value-list)))
+            ((string= kind "Type")
+             (setq type-list (cons marker type-list)))
+            ((string= kind "Exn")
+             (setq exception-list (cons marker exception-list))))
+      (when sub-trees
+        (merlin-imenu-parse-outline (concat prefix entry ".") sub-trees)))))
 
 (defun merlin-imenu-create-index ()
   "Create data for imenu using the merlin outline feature."

(require 'tuareg)
(require 'subr-x)
(require 'merlin)

;;; enable depth and size threshold for OCaml modules with big size
(setq max-lisp-eval-depth 10000)
(setq max-specpdl-size 10000)

;; lists of different outline items
(defvar-local value-list nil)

        type-list nil
        exception-list nil)
  ;; Read outline tree
  (merlin-imenu-parse-outline "" (merlin/call "outline"))
  (let ((index nil))
    (when value-list (push (cons "Value" value-list) index))
    (when exception-list (push (cons "Exception" exception-list) index))
    (when type-list (push (cons "Type" type-list) index))
