function (keys, values, rereduce)
{
    function unique(arr) {
        var a = [];
        var l = arr.length;
        for(var i=0; i<l; i++) {
            for(var j=i+1; j<l; j++) {
                if (arr[i] === arr[j])
                    j = ++i;
            }
            a.push(arr[i]);
        }
        return a;
    };

    function flatten(arr)
    {
        flattened = []
        for(n in arr)
        {
            for(x in arr[n])
            {
                flattened.push(arr[n][x]);
            }
        }
        return flattened
    };

    function sortNumerical(a, b)
    {
        if(a < b)
        {
            return -1;
        }
        else if (a > b)
        {
            return 1;
        }
        else
        {
            return 0;
        }
    };

    function mergeBorders(borders, maxBorders)
    {
        last = null;
        newBorders = [];

        lastBorder = borders[borders.length-1];
        minDist = lastBorder / maxBorders;

        for(var n in borders)
        {
            border = borders[n];

            if(last == null)
            {
            }
            else if (border == lastBorder)
            {
            }
            else if ((border - last) < minDist)
            {
                continue;
            }

            newBorders.push(border);
            last = border;
        }

        return newBorders;
    };

    if (rereduce)
    {
        all_borders = [];
        for(n in values)
        {
            all_borders.push(values[n].borders);
        }
    }
    else
    {
        all_borders = values;
    }

    borders = flatten(all_borders);
    borders = unique(borders);
    borders.sort(sortNumerical);
    borders = mergeBorders(borders, 50);

    heatmap = [];

    for(n=0; n<borders.length-1; n++)
    {
        heatmap.push(0);
    }

    for(n in values)
    {
        j = 0;

        if(rereduce)
        {
            length = values[n].borders.length-1;
            increment = 1;
            function heat_val(i) { return values[n].heatmap[i]; };
        }
        else
        {
            length = values[n].length;
            increment = 2;
            heat_val = 1;
            function heat_val(i) { return 1; };
        }


        for(i=0; i<length; i+=increment)
        {
            from  = values[n][i];
            until = values[n][i+1];

            while(borders[j] < from)
            {
                j++;
            }

            while(borders[j] < until)
            {
                heatmap[j++] += heat_val(i);
            }
        }
    }

    return {heatmap: heatmap, borders: borders};
}

@@ -354,32 +354,36 @@
 )%0A    %7B%0A        
+var 
 flattened = %5B%5D%0A 
@@ -389,24 +389,28 @@
         for(
+var 
 n in arr)%0A  
@@ -433,16 +433,20 @@
     for(
+var 
 x in arr
@@ -1396,24 +1396,51 @@
 rs;%0A    %7D;%0A%0A
+    var all_borders = %5B%5D;%0A%0A
     if (rere
@@ -1463,38 +1463,16 @@
     
-all_borders = %5B%5D;%0A        
 for(
+var 
 n in
@@ -1607,24 +1607,28 @@
 %0A    %7D%0A%0A    
+var 
 borders = fl
@@ -1753,24 +1753,28 @@
 , 50);%0A%0A    
+var 
 heatmap = %5B%5D
@@ -1784,16 +1784,20 @@
     for(
+var 
 n=0; n%3Cb
@@ -1864,16 +1864,20 @@
     for(
+var 
 n in val
@@ -1901,16 +1901,67 @@
   j = 0;
+%0A        var length = 0;%0A        var increment = 1;
 %0A%0A      
@@ -2065,75 +2065,8 @@
  1;%0A
-            function heat_val(i) %7B return values%5Bn%5D.heatmap%5Bi%5D; %7D;%0A
     
@@ -2172,24 +2172,91 @@
     
-    heat_val = 1
+%7D%0A%0A%0A        for(var i=0; i%3Clength; i+=increment)%0A        %7B%0A            var from = 0
 ;%0A  
@@ -2269,43 +2269,62 @@
     
-f
+var 
 un
-c
 ti
-on heat_val(i) %7B return 1; %7D;
+l = 0;%0A%0A            if(rereduce)%0A            %7B
 %0A   
@@ -2320,36 +2320,32 @@
       %7B%0A        
-%7D%0A%0A%0A
         for(i=0;
@@ -2341,50 +2341,134 @@
    f
-or(i=0; i%3Clength; i+=increment)%0A        %7B%0A
+rom  = values%5Bn%5D.borders%5Bi%5D;%0A                until = values%5Bn%5D.borders%5Bi+1%5D;%0A            %7D%0A            else%0A            %7B%0A    
     
@@ -2501,32 +2501,36 @@
 i%5D;%0A            
+    
 until = values%5Bn
@@ -2536,16 +2536,30 @@
 n%5D%5Bi+1%5D;
+%0A            %7D
 %0A%0A      
@@ -2711,36 +2711,210 @@
     
-heatmap%5Bj++%5D += heat_val(i);
+if(rereduce)%0A                %7B%0A                    heatmap%5Bj++%5D += values%5Bn%5D.heatmap%5Bi%5D;%0A                %7D%0A                else%0A                %7B%0A                    heatmap%5Bj++%5D += 1;%0A                %7D
 %0A   

fix episode_heatmap reduce func fail on rereduce