         }
 
         func bwt(transform data: Data) -> [Int] {
-            let bytes = data.toArray(type: UInt8.self).sorted()
-            var base: [Int] = Array(repeating: -1, count: data.count)
+            // TODO: Check performance with and without sorting.
+            let bytes = data.toArray(type: UInt8.self)
+            let sortedBytes = bytes.sorted()
+            var base: [Int] = Array(repeating: -1, count: 256)
             for i in 0..<256 {
-                base[i] = bytes.index(of: UInt8(truncatingBitPattern: UInt(i))) ?? -1
+                base[i] = sortedBytes.index(of: UInt8(truncatingBitPattern: UInt(i))) ?? -1
             }
 
             var pointers: [Int] = Array(repeating: -1, count: data.count)

        var t: HuffmanTable?

        while true {
            decoded -= 1
            if decoded <= 0 {
                decoded = 50

                }
            }

            guard let symbolLength = t?.findNextSymbol(in: data.bits(count: 24), reversed: false, straightBitOrder: true) else {
                throw BZip2Error.SymbolNotFound
            }
            data.rewind(bitsCount: 24 - symbolLength.bits)
            let symbol = symbolLength.code
            if symbol == 1 || symbol == 0 {
                if repeat_ == 0 {
                    repeatPower = 1
                }
