   docToNodeData: (doc, args) ->
     args = _.extend({
       children: true
+      visited: {count: 0, ids: {}}
     }, args)
+    return if args.visited.ids[doc._id]# || args.visited.count >= 200
+
+    args.visited.ids[doc._id] = true
+    args.visited.count++
     data =
       id: doc._id
       label: doc.name
     if args.children
       childrenDocs = @getChildren(doc)
-      childrenData = _.map childrenDocs, @docToNodeData, @
+      childrenData = []
+      _.each childrenDocs, (childDoc) =>
+        childData = @docToNodeData(childDoc, args)
+        if childData then childrenData.push(childData)
       data.children = childrenData
     data
 

  data = @data
  model = @model
  items = @items
  cursor = Collections.getCursor(items)
  settings = getSettings()
  template = @
  loadTree(@)

  # Only a reactive cursor can observe for changes. A simple array can only render a tree once.
  return unless cursor
  @autorun ->

    Collections.observe cursor,
      added: (newDoc) ->
        id = newDoc._id
        data = model.docToNodeData(newDoc, {children: false})
        docParentId = model.getParent(data)
        unless docParentId
          # There is a significant memory leak caused by re-loading the entire tree after each

    @remove(toRemove)
    toRemove

  toggle: (ids, enabled) ->
    existingIds = @getIds()
    toRemove = _.intersection(existingIds, ids)
    toAdd = _.difference(ids, existingIds)
