 calcRecall = (cm, index, firstInvalidIndex) ->
     tp = cm.data[index][index]
     fn = 0
-    for value, i in cm.data[index]
+    for column, i in cm.data
         if i >= firstInvalidIndex
             break
-        if i != index
-            fn += value
+        if i != index # if not on diagonal
+            fn += column[index]
     result = tp / (tp + fn)
     return parseFloat(result).toFixed(2).replace(/\.0+$/, '.0')
 
 calcPrecision = (cm, index, firstInvalidIndex) ->
     tp = cm.data[index][index]
     fp = 0
-    for column, i in cm.data # iterate over all columns
+    for value, i in cm.data[index]
         if i >= firstInvalidIndex # do not count Error, Rate and Recall columns
             break
         if i != index # if not on diagonal
-            fp += column[index] # pick value at index from each column; sum of row
+            fp += value
     result = tp / (tp + fp)
     return parseFloat(result).toFixed(2).replace(/\.0+$/, '.0')
 

    Flow.HTML.template("td.#{tdClasses}")(tooltipDiv)

renderMultinomialConfusionMatrix = (title, cm) ->
  cm.columns.push({'name':'Recall', 'type':'long', 'format': '%.2f', 'description': 'Recall'})
  errorColumnIndex = cm.columns.length - 3 # last three cols are Error, Rate Recall
  recallValues = []
  cm.rowcount += 1 # We will have new row with Precision values
  totalRowIndex = cm.rowcount - 2 # Last two rows will be Totals and Precision
  for column, i in cm.data
      if i < errorColumnIndex
          column.push(calcPrecision(cm, i, errorColumnIndex)) # calculate precision for each feature and add it as last row for each column
      if i < totalRowIndex
          recallValues.push(calcRecall(cm, i, totalRowIndex)) # calculate recall for each feature, will be added as new column
  cm.data.push(recallValues) # add recall values as new (last) column

  [table, tbody, tr, normal, bold] = Flow.HTML.template 'table.flow-confusion-matrix', 'tbody', 'tr', 'td', 'td.strong'
  tooltip = (tooltipText) ->

            if rowIndex is totalRowIndex
                tooltipBold("Total: #{cm.columns[i].description}") # Totals of features
            else
                if rowIndex is recallRowIndex
                    tooltipBold("Recall: #{cm.columns[i].description}") # Precision of features
                else
                    bold
      else
        if rowIndex < totalRowIndex
            tooltipBold("#{cm.columns[i].description}: #{cm.columns[rowIndex].description}") # Error, Rate and Recall of features
        else
            if rowIndex is totalRowIndex and i < precisionColumnIndex
                tooltipBold("Total: #{cm.columns[i].description}") # Totals of Error and Rate
            else
                bold
      # special-format error column
      cell if i is errorColumnIndex then format4f column[rowIndex] else column[rowIndex]
    # Add the corresponding column label
    cells.unshift bold if rowIndex is cm.rowcount - 2 then 'Total' else if rowIndex is cm.rowcount - 1 then 'Recall' else cm.columns[rowIndex].description
    rows.push tr cells

  return params = {
