                 return
             }
             
-            // TODO: simplify this
-            var authorsInRepoSet = Set<String>()
-            let keys = json.values.flatMap({ $0.keys })
-            for k in keys {
-                authorsInRepoSet.insert(k)
-            }
+            let authorsInRepoSet = Set(json.values.flatMap({ $0.keys }))
             let authorsInRepo = Array(authorsInRepoSet).sort()
             
             // draw repo name
-            c.drawText(repo, origin: P(LEFT_MARGIN_WIDTH, c.height() - Constants.TOP_MARGIN_HEIGTH - (repoStartIndex) * ROW_HEIGHT - 18), fontName: "Monaco", fontSize: 10)
-            
-            repoStartIndex += 1
+            c.drawText(repo, origin: P(LEFT_MARGIN_WIDTH, c.height() - self.TOP_MARGIN_HEIGTH - (currentRow) * ROW_HEIGHT - 18), fontName: "Monaco", fontSize: 10)
+            
+            currentRow += 1
             
             // draw authors
-            if let (_, weekday_offset) = sortedDayInfo.last {
-                let (_, offset) = weekday_offset
-                
-                for (authorIndex, author) in authorsInRepo.enumerate() {
-                    // draw author name
-                    c.drawText(
-                        author,
-                        origin: P(LEFT_MARGIN_WIDTH + offset + Constants.COL_WIDTH + 18, c.height() - Constants.TOP_MARGIN_HEIGTH - (repoStartIndex+authorIndex) * ROW_HEIGHT - 15),
-                        fontName: "Monaco",
-                        fontSize: 10)
-                    
-                }
-            }
-            
-            // draw background rectangles
-            
-            for (_, t) in sortedDayInfo {
-                
-                let (weekDay, offset) = t
-                
-                if weekDay == 1 || weekDay == 2 { continue }
-                
+            for (authorIndex, author) in authorsInRepo.enumerate() {
+                // draw author name
+                c.drawText(
+                    author,
+                    origin: P(legendAndAuthorsXPosition, c.height() - self.TOP_MARGIN_HEIGTH - (currentRow+authorIndex) * ROW_HEIGHT - 15),
+                    fontName: "Monaco",
+                    fontSize: 10)
+            }
+            
+            // draw background cells
+            let weekDayOffsetTuples = sortedDayInfo.map( { $0.1 } ).filter( { weekDaysToSkip.contains($0.0) == false } )
+            
+            for (_, offset) in weekDayOffsetTuples {
                 for (i, _) in authorsInRepo.enumerate() {
-                    if let rect = rectForDay (
-                        offset,
-                        rowIndex: repoStartIndex+i,
-                        canvasHeight: c.height()
-                        ) {
-                            let fillColor = NSColor.clearColor()
-                            
-                            c.drawRectangle(rect, strokeColor: NSColor.lightGrayColor(), fillColor: fillColor)
-                    }
+                    let rect = rectForDay (offset, rowIndex: currentRow+i, canvasHeight: c.height())
+                    c.drawRectangle(rect, strokeColor: NSColor.lightGrayColor(), fillColor: NSColor.clearColor())
                 }
             }
             
             // draw activity rectangles
             for (day, authorsDict) in json {
-                
                 for (author, addedRemovedDict) in authorsDict {
-                    
-                    // print("    ", author)
-                    // print("        ", addedRemovedDict["added"])
-                    // print("        ", addedRemovedDict["removed"])
-                    
+
                     var linesChanged = 0
                     
-                    if let added = addedRemovedDict["added"] {
-                        linesChanged += added
-                    }
-                    
-                    if let removed = addedRemovedDict["removed"] {
-                        linesChanged += removed
-                    }
+                    linesChanged +=? addedRemovedDict["added"]
+                    linesChanged +=? addedRemovedDict["removed"]
                     
                     if let (weekDay, offset) = daysInfo[day], indexOfAuthor = authorsInRepo.indexOf(author) {
                         
-                        if (weekDay == 1 || weekDay == 2) { continue }
+                        if weekDaysToSkip.contains(weekDay) { continue }
                         
-                        if let rect = rectForDay(
-                            offset,
-                            rowIndex: repoStartIndex+indexOfAuthor,
-                            canvasHeight: c.height()
-                            ) {
-                                let fillColor = fillColorForLineCountPerDay(linesChanged, baseColor:colorForAuthor(author))
-                                
-                                c.drawRectangle(rect, strokeColor: NSColor.lightGrayColor(), fillColor: fillColor)
-                        }
+                        let rect = rectForDay(offset, rowIndex: currentRow+indexOfAuthor, canvasHeight: c.height())
+                        let fillColor = fillColorForLineCountPerDay(linesChanged, baseColor:colorForAuthor(author))
+                        c.drawRectangle(rect, strokeColor: NSColor.lightGrayColor(), fillColor: fillColor)
                     }
                 }
             }
             
-            repoStartIndex += authorsInRepo.count
+            currentRow += authorsInRepo.count
         }
         
         c.saveAtPath(outPath)


import Cocoa

struct Chart {
    
    struct Constants {
        static let COL_WIDTH = 20
        static let ROW_HEIGHT = 20
        static let LEFT_MARGIN_WIDTH = 20
        static let TOP_MARGIN_HEIGTH = 100
    }
    
    var dateFormatter: NSDateFormatter = {
        let df = NSDateFormatter()

            return
        }
        
        let daysInfo = daysInfoFromDay(fromDay, toDay:toDay)
        
        let sortedDayInfo = daysInfo.sort { return $0.0 < $1.0 }
        
        // draw days
        for (day, v) in daysInfo {
            let (weekDay, offset) = v
            if (weekDay == 1 || weekDay == 2) { continue }
            let p = P(LEFT_MARGIN_WIDTH + offset, c.height() - self.TOP_MARGIN_HEIGTH)
            c.drawText("\(day)", origin: P(p.x-13, p.y+35), fontName: "Monaco", fontSize: 10, rotationAngle: CGFloat(M_PI/2.0))
        }
        
        // find legend x position
        guard let (_, (_, offset)) = sortedDayInfo.last else { assertionFailure("period must be at least 1 day"); return }
        let legendAndAuthorsXPosition = LEFT_MARGIN_WIDTH + offset + COL_WIDTH + 18
        
        // draw legend
        self.drawLegend(c, x: legendAndAuthorsXPosition)
        
        var currentRow = 0
        
        for (repo, jsonPath) in repoTuples {
            
