     result = mat[0][0]
     for i of mat
         for j of mat[i]
-            result = mat[i][j] if mat[i][j]? and result > mat[i][j]
+            result = mat[i][j] if !(result?) or (result > mat[i][j] and mat[i][j]?)
     result
 
 matrixMax = (mat) ->
     result = mat[0][0]
     for i of mat
         for j of mat[i]
-            result = mat[i][j] if mat[i][j]? and result < mat[i][j]
+            result = mat[i][j] if !(result?) or (result < mat[i][j] and mat[i][j]?)
     result
 
 matrixMaxAbs = (mat) ->
     result = Math.abs(mat[0][0])
     for i of mat
         for j of mat[i]
-            result = Math.abs(mat[i][j]) if mat[i][j]? and result < Math.abs(mat[i][j])
+            result = Math.abs(mat[i][j]) if !(result?) or (result < Math.abs(mat[i][j]) and mat[i][j]?)
     result
 
 matrixExtent = (mat) -> [matrixMin(mat), matrixMax(mat)]

pullVarAsArray = (data, variable) ->
    (data[i][variable] for i of data)

# Select a set of categorical colors
# ngroup is positive integer
# palette = "dark" or "pastel"

    if palette == "dark"
        return ["slateblue"] if ngroup == 1
        return ["MediumVioletRed", "slateblue"] if ngroup == 2
        return ["MediumVioletRed", "MediumSeaGreen", "slateblue"] if ngroup == 3
        return colorbrewer.Set1[ngroup] if ngroup <= 9
        return d3.scale.category20().range()[0...ngroup]
    else
