         }
 
         bitWriter.write(number: tables.count, bitsCount: 3)
-        bitWriter.write(number: selectorsUsed, bitsCount: 15)
-
-        let mtfSelectors = mtf(selectors)
+        bitWriter.write(number: selectors.count, bitsCount: 15)
+
+        let mtfSelectors = mtf(selectors, characters: Array(0..<selectors.count))
         for selector in mtfSelectors {
             guard selector <= 5
                 else { fatalError("Incorrect selector.") }

        }
    }

    private static func mtf(_ array: [Int]) -> [Int] {
        var result = [Int]()
        var mtf = Array(0..<array.count)
        for i in 0..<array.count {
            let index = mtf.index(of: array[i])!
            result.append(index)
            let old = mtf.remove(at: index)
            mtf.insert(old, at: 0)
        }
        return result
    }

}

        var encoded = 0
        var selectorPointer = 0
        var t: EncodingHuffmanTree?
        for symbol in out {
            encoded -= 1
            if encoded <= 0 {
                encoded = 50
