                             repeatIndex = matchStartIndex + 1
                         }
                     }
-                    let ld = LengthDistance(matchLength, distance)
-                    buffer.append(BLDCode.lengthDistance(ld))
-                    stats[ld.lengthSymbol] += 1
-                    stats[286 + ld.distanceSymbol] += 1
+                    buffer.append(BLDCode.lengthDistance(UInt16(truncatingBitPattern: matchLength), UInt16(truncatingBitPattern: distance)))
+                    stats[Constants.lengthCode[matchLength - 3]] += 1 // Length symbol.
+                    stats[286 + ((Constants.distanceBase.index { $0 > distance }) ?? 30) - 1] += 1 // Distance symbol.
                     inputIndex += matchLength
                 } else {
                     buffer.append(BLDCode.byte(byte))

            switch code {
            case .byte(let byte):
                try mainLiterals.code(symbol: byte.toInt(), &bitWriter, DeflateError.symbolNotFound)
            case .lengthDistance(let ld):
                try mainLiterals.code(symbol: ld.lengthSymbol, &bitWriter, DeflateError.symbolNotFound)
                bitWriter.write(number: ld.lengthExtraBits, bitsCount: ld.lengthExtraBitsCount)

                try mainDistances.code(symbol: ld.distanceSymbol, &bitWriter, DeflateError.symbolNotFound)
                bitWriter.write(number: ld.distanceExtraBits, bitsCount: ld.distanceExtraBitsCount)
            }
        }


        return bitWriter.buffer
    }

    private enum BLDCode {
        case byte(UInt8)
        case lengthDistance(UInt16, UInt16)
    }

    private static func lengthEncode(_ rawBytes: [UInt8]) -> (codes: [BLDCode], stats: [Int]) {
