         lesser = if nodes[left].value > nodes[right].value then right else left
         if node.value > nodes[lesser].value
           nodes[i] = nodes[lesser]
+          nodes[i].index = i
           nodes[lesser] = node
+          nodes[lesser].index = lesser
           return lesser
       else if node.value > nodes[left].value
         nodes[i] = nodes[left]
+        nodes[i].index = i
         nodes[left] = node
+        nodes[left].index = left
         return left
     return null
 
-  heapify: ()->
-    for i in [0..@nodes.length-1]
-      @siftDown(i)
+#  heapify: ()->
+#    for i in [0..@nodes.length-1]
+#      @siftDown(i)
 
 module.exports = Heap
  nodes: null
  length: 0
  constructor: (options)->
    if !options?
      options = {}
    @nodes = options.nodes || []
    @heapify()

  pop: ()->
    if @length > 0

    if content
      length = @length++
      nodes = @nodes
      #objects can onyl be part of one heap at a time, bad things happen otherwise
      content._heapNode = nodes[length] = 
        content: content
        value: value
        index: length
      @bubbleUp(length)

  update: (content, value)->
    if content? && content._heapNode?
      nodes = @nodes
      swap = content._heapNode

      i = swap.index
      nodes[i] = nodes[0]
      nodes[i].index = i
      nodes[0] = swap
      nodes[0].index = 0
      swap.value = value

      sift = 0
