     # integration function on each node that only pulls values from immediate parents.
     # Note that this "pushing" may not do anything of value in a closed loop, as the values
     # will simply move around the circle.
-    # We then run the simulation an additional 20 times, and the average the 20 results to
+    # We then run the simulation an additional 20 times, and average the 20 results to
     # obtain a final value.
-    # The number "20" used is arbitrary, but large enough not to affect loops the we expect
+    # The number "20" used is arbitrary, but large enough not to affect loops that we expect
     # to see in Sage. In any loop, if the number of nodes in the loop and the number of times
-    # we iterate are not dividible by each other, we'll see imbalances, but the effect of the
+    # we iterate are not divisible by each other, we'll see imbalances, but the effect of the
     # imbalance is smaller the more times we loop around.
 
     # Changes to accomodate data flows:
     #
-    # There are now two types of nodes: normal and transfer and four types of links: range,
-    # accumulator, transfer and transfer-modifier.  Transfer nodes are created automatically
+    # There are now three types of nodes: normal, collector, and transfer and four types of links:
+    # range, accumulator, transfer and transfer-modifier.  Transfer nodes are created automatically
     # between two accumulator nodes when the link type is set to transfer and are automatically
-    # removed is the link is changed away from transfer or either of the nodes in the link
+    # removed if the link is changed away from transfer or either of the nodes in the link
     # is changed from not being an accumulator.  Range links are the type of the original links -
     # they are pure functions that transmit a value from a source (domain) to a target (range) node
     # and are the only links evaluated during the 20 step cumulative value calculation.
-    # Once each node's cummulative value obtained and then averaged across the nodes the accumulator
+    # Once each node's cumulative value is obtained and then averaged across the nodes, the accumulator
     # values are updated by checking the accumulator and transfer links into any accumulator node.
     # The transfer links values are then modified by the transfer-modifier links which are links
-    # from the source node of a transfer link to the tranfer node of the transfer link.
+    # from the source node of a transfer link to the transfer node of the transfer link.
 
     step = =>
       # push values down chain

  value

SetAccumulatorValueFunction = (nodeValues) ->
  # get add, subtract and transfer links
  links = @inLinks('accumulator').concat(@inLinks('transfer'))
  return unless links.length > 0

  value = 0
  for link in links
    {sourceNode, relation, transferNode} = link
    inV = nodeValues[sourceNode.key]
    switch relation.type
      when 'accumulator'
        # TODO: check if outV is really equal to inV here - I'm not clear what outV is in this case'
        value += relation.evaluate(inV, inV, sourceNode.max, @max)

      when 'transfer'
        # TODO: check with Dan if the transfer value really is meant to be a percentage
        transferValue = nodeValues[transferNode.key]
        transferPercentage = transferValue / Math.max(transferNode.max, transferValue)
        for modifierLink in transferNode.inLinks('transfer-modifier')
          transferPercentage *= modifierLink.relation.evaluate(inV, inV, sourceNode.max, transferNode.max)

        finalTransferValue = inV * transferPercentage
        value += finalTransferValue
        # TODO: check with Dan if we take 1/100th of the value or 1/Math.pow(100,links.length)
        sourceValue = sourceNode.currentValue - (finalTransferValue / 100)
        sourceNode.currentValue = sourceNode.filterFinalValue sourceValue

  delta = value / Math.pow(100, links.length)
  # accumulators hold their values in previousValue which is confusing
  # (this done because the accumulator values is only computed on the first of the 20 loops in RangeIntegrationFunction)
  # TODO: possibly change RangeIntegrationFunction function to make this more clear
  @currentValue = @filterFinalValue @previousValue + delta

module.exports = class Simulation


        node._cumulativeValue = 0

      # set the accumulator values
      collectorNodes = _.filter @nodes, (node) -> node.isAccumulator
      _.each collectorNodes, (node) ->
        node.setAccumulatorValue nodeValues

      time++
      @generateFrame(time)
