       }
 
       if !currentPositionIsInBeam {
-        for notes in notesInBeam.grouped(4) {
-          for group in notes.spanBy({$0.1.length}) {
-            if group.count == 1 {
-              let (rect, note) = group.first!
-              let v = BlackNote(frame: rect)
-              let b = Block()
-              let lineHeight = staffInterval * 3
-              let lineWidth = layout.staffLineWidth * 2
-
-              var flagFrame: CGRect
-              let invert = shouldInvert(note.pitch)
-
-              if (invert) {
-                b.frame = CGRectMake(rect.origin.x, rect.origin.y + rect.size.height * 0.6, lineWidth, lineHeight)
-                let y = rect.origin.y + rect.size.height
-                flagFrame = CGRectMake(rect.origin.x, y, rect.size.width, b.frame.size.height - y + b.frame.origin.y)
-              } else {
-                b.frame = CGRectMake(rect.origin.x + rect.size.width - lineWidth, rect.origin.y - lineHeight + rect.size.height * 0.4, lineWidth, lineHeight)
-                flagFrame = CGRectMake(rect.origin.x + rect.size.width, b.frame.origin.y, rect.size.width, rect.origin.y - b.frame.origin.y)
-              }
-
-              let length = note.length.actualLength(tuneHeader.unitNoteLength.denominator)
-              if length >= 0.125 {
-                let flag = FlagEighth(frame: flagFrame)
-                flag.invert = invert
-                canvas.addSubview(flag)
-              } else {
-                let flag = FlagSixteenth(frame: flagFrame)
-                flag.invert = invert
-                canvas.addSubview(flag)
-              }
-
-              canvas.addSubview(b)
-              canvas.addSubview(v)
-            } else if group.nonEmpty {
-              let lowest = group.maxBy({$0.0.origin.y})! // maxBy is correct.
-              let highest = group.minBy({$0.0.origin.y})! // minBy is correct.
-
-              let first = group.first!
-              let last = group.last!
-
-              let a = (last.0.origin.y - first.0.origin.y) / (last.0.origin.x - first.0.origin.x)
-              let slope = a.abs < layout.maxBeamSlope ? a : layout.maxBeamSlope * a.sign
-
-              let upperF = linearFunction(slope,
-                point: Point2D(x: highest.0.origin.x + highest.0.size.width, y: highest.0.origin.y - layout.minStemHeight))
-              let lowerF = linearFunction(slope,
-                point: Point2D(x: lowest.0.origin.x, y: lowest.0.origin.y + lowest.0.size.height + layout.minStemHeight))
-
-              let staffYCenter = staffTop + staffInterval * 2
-              let upperDiff = group.map({ abs(staffYCenter - upperF($0.0.origin.x + $0.0.size.width)) }).sum()
-              let lowerDiff = group.map({ abs(staffYCenter - lowerF($0.0.origin.x)) }).sum()
-              let rightDown = first.0.origin.y < last.0.origin.y
-
-              let beam = Beam()
-              beam.lineWidth = layout.staffLineWidth * 5
-
-              if (upperDiff < lowerDiff) {
-                for (r, _) in group {
-                  let b = Block()
-                  let x = r.origin.x + r.size.width - layout.staffLineWidth * 2
-                  let y = upperF(x)
-                  b.frame = CGRectMake(x, y, layout.staffLineWidth * 2, r.origin.y - y + r.size.height * 0.4)
-                  canvas.addSubview(b)
-                }
-
-                let x1 = first.0.origin.x + first.0.size.width - layout.staffLineWidth * 2
-                let y1 = upperF(x1)
-                let x2 = last.0.origin.x + last.0.size.width
-                let y2 = upperF(x2)
-                beam.rightDown = rightDown
-                beam.frame = CGRectMake(x1, min(y1, y2), x2 - x1, max(abs(y1 - y2), layout.staffLineWidth * 5))
-
-                if first.1.length.actualLength(tuneHeader.unitNoteLength.denominator) <= 0.0625 {
-                  let beam2 = Beam(frame: CGRectMake(beam.frame.origin.x, beam.frame.origin.y + beam.lineWidth * 1.5 , beam.frame.size.width, beam.frame.size.height))
-                  beam2.lineWidth = beam.lineWidth
-                  beam2.rightDown = rightDown
-                  canvas.addSubview(beam2)
-                }
-              } else {
-                for (r, _) in group {
-                  let b = Block()
-                  let x = r.origin.x
-                  let y = lowerF(x)
-                  b.frame = CGRectMake(x, r.origin.y + r.size.height * 0.6, layout.staffLineWidth * 2, y - r.origin.y - r.size.height * 0.6)
-                  canvas.addSubview(b)
-                }
-
-                let x1 = first.0.origin.x
-                let y1 = lowerF(x1)
-                let x2 = last.0.origin.x + layout.staffLineWidth * 2
-                let y2 = lowerF(x2)
-                beam.rightDown = rightDown
-                beam.frame = CGRectMake(x1, min(y1, y2), x2 - x1, max(abs(y1 - y2), layout.staffLineWidth * 5))
-
-                if first.1.length.actualLength(tuneHeader.unitNoteLength.denominator) <= 0.0625 {
-                  let beam2 = Beam(frame: CGRectMake(beam.frame.origin.x, beam.frame.origin.y - beam.lineWidth * 1.5 , beam.frame.size.width, beam.frame.size.height))
-                  beam2.lineWidth = beam.lineWidth
-                  beam2.rightDown = rightDown
-                  canvas.addSubview(beam2)
-                }
-              }
-
-              canvas.addSubview(beam)
-            }
+        renderNotesInBeam(notesInBeam, unitDenominator: tuneHeader.unitNoteLength.denominator)
+        notesInBeam = []
+      }
+    }
+
+    if notesInBeam.nonEmpty { renderNotesInBeam(notesInBeam, unitDenominator: tuneHeader.unitNoteLength.denominator) }
+  }
+
+  private func renderNotesInBeam(notesInBeam: [(CGRect, Note)], unitDenominator: UnitDenominator) -> Void {
+    for notes in notesInBeam.grouped(4) {
+      for group in notes.spanBy({$0.1.length}) {
+        if group.count == 1 {
+          let (rect, note) = group.first!
+          let noteHead = BlackNote(frame: rect)
+          let stem = Block()
+          let lineHeight = staffInterval * 3
+          let lineWidth = layout.stemWidth
+
+          var flagFrame: CGRect
+          let invert = shouldInvert(note.pitch)
+
+          if (invert) {
+            stem.frame = CGRect(x: rect.x, y: rect.y + rect.height * 0.6, width: lineWidth, height: lineHeight)
+            flagFrame = CGRect(x: rect.x, y: rect.leftBottom.y, width: rect.width, height: stem.frame.leftBottom.y - rect.leftBottom.y)
+          } else {
+            stem.frame = CGRect(x: rect.rightTop.x - lineWidth, y: rect.y - lineHeight + rect.height * 0.4, width: lineWidth, height: lineHeight)
+            flagFrame = CGRect(x: rect.rightTop.x, y: stem.frame.y, width: rect.width, height: rect.y - stem.frame.y)
           }
+
+          let length = note.length.actualLength(unitDenominator)
+          if length >= Eighth {
+            let flag = FlagEighth(frame: flagFrame)
+            flag.invert = invert
+            canvas.addSubview(flag)
+            for d in dots(note.pitch, x: rect.rightTop.x, length: length, denominator: Eighth) { canvas.addSubview(d) }
+          } else if length >= Sixteenth {
+            let flag = FlagSixteenth(frame: flagFrame)
+            flag.invert = invert
+            canvas.addSubview(flag)
+            for d in dots(note.pitch, x: rect.rightTop.x, length: length, denominator: Sixteenth) { canvas.addSubview(d) }
+          }
+
+          canvas.addSubview(stem)
+          canvas.addSubview(noteHead)
+
+        } else if group.nonEmpty {
+          let (lowestRect, _) = group.maxBy({$0.0.origin.y})! // maxBy is correct.
+          let (highestRect, _) = group.minBy({$0.0.origin.y})! // minBy is correct.
+
+          let (firstRect, firstNote) = group.first!
+          let (lastRect, _) = group.last!
+
+          let a = (lastRect.y - firstRect.y) / (lastRect.x - firstRect.x)
+          let slope = a.abs < layout.maxBeamSlope ? a : layout.maxBeamSlope * a.sign
+
+          let upperF = linearFunction(slope,
+            point: Point2D(x: highestRect.rightTop.x, y: highestRect.y - layout.minStemHeight))
+          let lowerF = linearFunction(slope,
+            point: Point2D(x: lowestRect.x, y: lowestRect.leftBottom.y + layout.minStemHeight))
+
+          let staffYCenter = staffTop + staffInterval * 2
+          let upperDiff = group.map({ abs(staffYCenter - upperF($0.0.origin.x + $0.0.size.width)) }).sum()
+          let lowerDiff = group.map({ abs(staffYCenter - lowerF($0.0.origin.x)) }).sum()
+          let rightDown = firstRect.y < lastRect.y
+
+          let beam = Beam()
+          beam.lineWidth = layout.beamLineWidth
+
+          if (upperDiff < lowerDiff) {
+            for (r, _) in group {
+              let x = r.rightTop.x - layout.staffLineWidth * CGFloat(staffNum / 2)
+              let y = upperF(x)
+              canvas.addSubview(Block(frame:
+                CGRect(x: x, y: y, width: layout.stemWidth, height: r.y - y + r.height * 0.4)))
+            }
+
+            let x1 = firstRect.rightTop.x - layout.stemWidth
+            let y1 = upperF(x1)
+            let x2 = lastRect.rightTop.x
+            let y2 = upperF(x2)
+            beam.rightDown = rightDown
+            beam.frame = CGRectMake(x1, min(y1, y2), x2 - x1, max(abs(y1 - y2), layout.beamLineWidth))
+
+            if firstNote.length.actualLength(unitDenominator) <= Sixteenth {
+              let beam2 = Beam(frame: beam.frame.withY(beam.frame.y + beam.lineWidth * 1.5))
+              beam2.lineWidth = beam.lineWidth
+              beam2.rightDown = rightDown
+              canvas.addSubview(beam2)
+            }
+          } else {
+            for (r, _) in group {
+              let x = r.x
+              let y = lowerF(x)
+              canvas.addSubview(Block(frame:
+                CGRect(x: x, y: r.y + r.height * 0.6, width: layout.stemWidth, height: y - r.y - r.height * 0.6)))
+            }
+
+            let x1 = firstRect.x
+            let y1 = lowerF(x1)
+            let x2 = lastRect.x + layout.stemWidth
+            let y2 = lowerF(x2)
+            beam.rightDown = rightDown
+            beam.frame = CGRectMake(x1, min(y1, y2), x2 - x1, max(abs(y1 - y2), layout.beamLineWidth))
+
+            if firstNote.length.actualLength(unitDenominator) <= Sixteenth {
+              let beam2 = Beam(frame: beam.frame.withY(beam.frame.y - beam.lineWidth * 1.5))
+              beam2.lineWidth = beam.lineWidth
+              beam2.rightDown = rightDown
+              canvas.addSubview(beam2)
+            }
+          }
+
+          canvas.addSubview(beam)
         }
-        notesInBeam = []
       }
     }
   }

  public let minStemHeight: CGFloat
  public let maxBeamSlope: CGFloat
  public let dotMarginLeft: CGFloat

  public init(
    staffHeight: CGFloat,
    staffLineWidth: CGFloat,
    stemWidth: CGFloat,
    widthPerUnitNoteLength: CGFloat,
    barMarginRight: CGFloat,
    minStemHeight: CGFloat,
    maxBeamSlope: CGFloat,
    dotMarginLeft: CGFloat) {
      self.staffHeight = staffHeight
      self.staffLineWidth = staffLineWidth
      self.stemWidth = stemWidth


    CGContextSetLineWidth(ctx, layout.staffLineWidth)
    CGContextSetStrokeColorWithColor(ctx, UIColor.lightGrayColor().CGColor)
    for i in 0..<staffNum {
      let offset = staffInterval * CGFloat(i)

      CGContextMoveToPoint(ctx, 0, top + offset)
