                      result)))))
     result))
 
-(defun configuration-layer//get-implicit-packages ()
-  "Returns a list of all packages in `packages-alist' which are not found
-in `configuration-layer-packages'"
-  (let ((imp-pkgs))
+(defun configuration-layer//get-implicit-packages (packages)
+  "Returns packages in `packages-alist' which are not found in PACKAGES."
+  (let (imp-pkgs)
     (dolist (pkg package-alist)
       (let ((pkg-sym (car pkg)))
-        (unless (object-assoc pkg-sym :name configuration-layer-packages)
+        (unless (object-assoc pkg-sym :name packages)
           (add-to-list 'imp-pkgs pkg-sym))))
     imp-pkgs))
 
 (defun configuration-layer//get-orphan-packages (implicit-pkgs dependencies)
-  "Return a list of all orphan packages which are basically meant to be
-deleted safely."
-  (let ((result '()))
+  "Return orphan packages."
+  (let (result)
     (dolist (imp-pkg implicit-pkgs)
-      (if (configuration-layer//is-package-orphan imp-pkg dependencies)
-          (add-to-list 'result imp-pkg)))
+      (when (configuration-layer//is-package-orphan imp-pkg dependencies)
+        (add-to-list 'result imp-pkg)))
     result))
 
-(defun configuration-layer//is-package-orphan (pkg dependencies)
-  "Returns not nil if PKG is an orphan package."
-  (if (object-assoc pkg :name configuration-layer-packages)
-      nil
-    (if (ht-contains? dependencies pkg)
-        (let ((parents (ht-get dependencies pkg)))
+(defun configuration-layer//is-package-orphan (pkg-name dependencies)
+  "Returns not nil if PKG-NAME is the name of an orphan package."
+  (unless (object-assoc pkg-name :name configuration-layer-packages)
+    (if (ht-contains? dependencies pkg-name)
+        (let ((parents (ht-get dependencies pkg-name)))
           (reduce (lambda (x y) (and x y))
                   (mapcar (lambda (p) (configuration-layer//is-package-orphan
                                        p dependencies))
                           parents)
                   :initial-value t))
-      (not (ht-contains? configuration-layer-packages pkg)))))
+      (not (object-assoc pkg-name :name configuration-layer-packages)))))
 
 (defun configuration-layer//get-package-directory (pkg)
   "Return the directory path for PKG."

                      (and (not (memq x spacemacs-used-theme-packages))
                           x)) orphans)))

(defun configuration-layer/delete-orphan-packages ()
  "Delete all the orphan packages."
  (interactive)
  (let* ((dependencies (configuration-layer//get-packages-dependencies))
         (implicit-packages (configuration-layer//get-implicit-packages))
         (orphans (configuration-layer//filter-used-themes
                   (configuration-layer//get-orphan-packages implicit-packages
                                                             dependencies)))

  "Return the path for LAYER symbol."
  (ht-get configuration-layer-paths layer))

(defun configuration-layer//get-all-packages-dependencies ()
  "Returns dependencies hash map for all packages in `package-alist'."
  (let ((result (make-hash-table :size 512)))
    (dolist (pkg package-alist)
      (let* ((pkg-sym (car pkg))
