       while i < node.childNodes.length
         @cleanTree node.childNodes[i]
         if not node.childNodes[i].__location
+          if node.childNodes[i].childNodes?
+            for child in node.childNodes[i].childNodes
+              child.parentNode = node
           node.childNodes = node.childNodes[0...i].concat(node.childNodes[i].childNodes || []).concat node.childNodes[i+1...]
         else
           i++
 
-  fixBounds: (node, lastPosition) ->
+  fixBounds: (node) ->
 
     if not node
+      return
+
+    if node.childNodes?
+      for child, i in node.childNodes
+        @fixBounds child
+      newList = []
+      for child in node.childNodes
+        if !child.remove
+          newList.push child
+      node.childNodes = newList
+
+    if node.nodeName is '#document' or node.nodeName is '#document-fragment'
+      node.type = 'document'
       return
 
     if node.nodeName is '#text'

      if node.value.trim().length is 0
        node.remove = true
      else
        @trimText node, lastPosition
      return

    if node.nodeName is '#comment'

        @setAttribs node, @text[node.__location.start...node.__location.end]
      return

    node.type = 'blockTag'

    node.__indentLocation = {start: node.__location.startTag.end}
