private function cleanNodes(DOMElement $elem, $isFirstNode = false)
    {
        $nodeName = strtolower($elem->nodeName);
        $textContent = $elem->textContent;
        if ($isFirstNode || array_key_exists($nodeName, $this->config->WhiteListTag)) {
            if ($elem->hasAttributes()) {
                $this->cleanAttributes($elem);
            }
            /*
             * Iterate over the element's children. The reason we go backwards is because
             * going forwards will cause indexes to change when elements get removed
             */
            if ($elem->hasChildNodes()) {
                $children = $elem->childNodes;
                $index = $children->length;
                while (--$index >= 0) {
                    $cleanNode = $children->item($index);// DOMElement or DOMText
                    if ($cleanNode instanceof DOMElement) {
                        $this->cleanNodes($cleanNode);
                    }
                }
            } else {
                if (!in_array($nodeName, $this->emptyElementList) && !$this->isValidText($textContent)) {
                    $elem->nodeValue = $this->TEMP_CONTENT;
                }
            }
        } else {
            if ($this->config->KeepText === true && $this->isValidText($textContent)) {
                $result = $elem->parentNode->replaceChild($this->dom->createTextNode($textContent), $elem);
            } else {
                $result = $elem->parentNode->removeChild($elem);
            }
            if ($result) {
                $this->removedTags[] = $nodeName;
            } else {
                throw new Exception('Failed to remove node from DOM');
            }
        }
    }