         }
 
         // Calculate maximum amount of leaves possible in a tree.
-        self.leafCount = Int(pow(Double(2), Double(lengths.last!.bits + 1)))
+        self.leafCount = Int(pow(Double(2), Double(lengths.last![1] + 1)))
         // Create a tree (array, actually) with all leaves equal nil.
         self.tree = Array(repeating: nil, count: leafCount)
 

            let endbits = pair[1]
            if bits > 0 {
                lengths.append(contentsOf:
                    (start..<finish).map { HuffmanLength(code: $0, bits: bits) })
            }
            start = finish
            bits = endbits
        }
        // Sort the lengths' array to calculate symbols correctly.
        lengths.sort()

        func reverse(bits: Int, in symbol: Int) -> Int {
            // Auxiliarly function, which generates reversed order of bits in a number.

        for length in lengths {
            symbol += 1
            // We sometimes need to make symbol to have length.bits bit length.
            let bits = length[1]
            if bits != loopBits {
                symbol <<= (bits - loopBits)
                loopBits = bits
            }
            // Then we need to reverse bit order of the symbol.
            var treeCode = reverse(bits: loopBits, in: symbol)
            // Finally, we put it at its place in the tree.
            var index = 0
            for _ in 0..<bits {
                let bit = treeCode & 1
                index = bit == 0 ? 2 * index + 1 : 2 * index + 2
                treeCode >>= 1
            }
            self.tree[index] = length[0]
        }
    }

