         guard let _ = try? data.decode(BEInt16.self) else { return nil }
         guard let _ = try? data.decode(BEInt16.self) else { return nil }
         
-        let count = Int(numberOfContours)
-        
-        guard let endPtsOfContours = try? (0..<count).map({ _ in try data.decode(BEUInt16.self) }) else { return nil }
+        guard let endPtsOfContours = try? (0..<Int(numberOfContours)).map({ _ in try data.decode(BEUInt16.self) }) else { return nil }
         
         guard let instructionLength = try? data.decode(BEUInt16.self) else { return nil }
         
         guard let instructions = try? (0..<Int(instructionLength)).map({ _ in try data.decode(UInt8.self) }) else { return nil }
         
+        let numberOfCoordinates = Int(endPtsOfContours.last!) + 1
+        
         var flags = [UInt8]()
-        flags.reserveCapacity(count)
-        while flags.count < count {
+        flags.reserveCapacity(numberOfCoordinates)
+        while flags.count < numberOfCoordinates {
             guard let flag = try? data.decode(UInt8.self) else { return nil }
             flags.append(flag)
             if flag & 8 != 0 {

                if flag & bitMask.1 != 0 {
                    code = previousValue
                } else {
                    code = try previousValue + data.decode(Int16.self)
                }
            }
            return code

    private func _glyfData(glyph: Int) -> Data? {
        precondition(glyph < numberOfGlyphs, "Index out of range.")
        guard let loca = self.loca, let glyf = self.glyf else { return nil }
        let startIndex: Int
        let endIndex: Int
        if head.indexToLocFormat == 0 {
            startIndex = Int(loca.withUnsafeBytes { $0[glyph] as BEUInt16 }) << 1
            endIndex = Int(loca.withUnsafeBytes { $0[glyph + 1] as BEUInt16 }) << 1
        } else {
            startIndex = Int(loca.withUnsafeBytes { $0[glyph] as BEUInt32 })
            endIndex = Int(loca.withUnsafeBytes { $0[glyph + 1] as BEUInt32 })
        }
        return glyf.dropFirst(startIndex).prefix(endIndex - startIndex)
    }
    
    func boundary(glyph: Int) -> Rect {
