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

      }

      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)
            }
          }
        }
        notesInBeam = []
      }
    }
  }

    return (pitch.offset * 7) + pitch.name.rawValue >= 0 * 7 + PitchName.B.rawValue
  }

  private func outsideStaff(x: CGFloat, pitch: Pitch) -> [Block] {
    let p = (pitch.offset * 7) + pitch.name.rawValue
    let upperBound = 1 * 7 + PitchName.A.rawValue
    let lowerBound = 0 * 7 + PitchName.C.rawValue
    let rect = noteHeadFrameFor(pitch, xOffset: x)
    var staff = [Block]()

    let x = rect.x - (layout.outsideStaffLineXLength - rect.width) / 2
    if p >= upperBound {
      for i in 1...(1 + (p - upperBound).abs / 2) {
        let y = staffTop - (CGFloat(i) * staffInterval)
        staff.append(Block(frame: CGRect(x: x, y: y, width: layout.outsideStaffLineXLength, height: layout.outsideStaffLineWidth)))
      }
    } else if p <= lowerBound {
      for i in 1...(1 + (p - lowerBound).abs / 2) {
        let y = staffTop + layout.staffHeight - layout.staffLineWidth + (CGFloat(i) * staffInterval)
        staff.append(Block(frame: CGRect(x: x, y: y, width: layout.outsideStaffLineXLength, height: layout.outsideStaffLineWidth)))
      }
    }

