diff --git a/contrib/lisp/org-e-html.el b/contrib/lisp/org-e-html.el
index 57542594d..94f42ea64 100644
--- a/contrib/lisp/org-e-html.el
+++ b/contrib/lisp/org-e-html.el
@@ -1123,8 +1123,7 @@ that uses these same face definitions."
(format "%s" todo-type headline)))))
(defun org-e-html-toc (depth info)
- (let* ((headlines (org-export-collect-headlines
- info (and (wholenump depth) depth)))
+ (let* ((headlines (org-export-collect-headlines info depth))
(toc-entries
(loop for headline in headlines collect
(list (org-e-html-format-headline--wrap
diff --git a/contrib/lisp/org-export.el b/contrib/lisp/org-export.el
index 665c54f60..67ec9acba 100644
--- a/contrib/lisp/org-export.el
+++ b/contrib/lisp/org-export.el
@@ -4075,17 +4075,20 @@ return nil."
INFO is a plist used as a communication channel.
-When non-nil, optional argument N must be an integer. It
-specifies the depth of the table of contents.
+When optional argument N is an integer, it specifies the depth of
+the table of contents. Otherwise, it is set to the value of the
+last headline level. See `org-export-headline-levels' for more
+information.
Return a list of all exportable headlines as parsed elements."
+ (unless (wholenump n) (setq n (plist-get info :headline-levels)))
(org-element-map
(plist-get info :parse-tree)
'headline
(lambda (headline)
;; Strip contents from HEADLINE.
(let ((relative-level (org-export-get-relative-level headline info)))
- (unless (and n (> relative-level n)) headline)))
+ (unless (> relative-level n) headline)))
info))
(defun org-export-collect-elements (type info &optional predicate)