                 index = bit == 0 ? 2 * index + 1 : 2 * index + 2
                 treeCode >>= 1
             }
-            self.tree[index] = length
-            self.newTree[index] = .leaf(length[0])
+            self.tree[index] = .leaf(length[0])
         }
 
         if coding {
             for treeIndex in stride(from: self.leafCount - 1, through: 0, by: -1) {
-                switch self.newTree[treeIndex] {
+                switch self.tree[treeIndex] {
                 case .leaf(let symbol):
                     if symbol == -1 {
                         var replacementArray = [Int]()
 
                         let leftChildIndex = 2 * treeIndex + 1
                         if leftChildIndex < self.leafCount {
-                            switch self.newTree[leftChildIndex] {
+                            switch self.tree[leftChildIndex] {
                             case .leaf(let leftSymbol):
                                 replacementArray.append(leftSymbol)
                             case .branch(let leftArray):

        var index = 0
        var bits = [UInt8]()
        while true {
            switch self.newTree[index] {
            case .leaf(let foundSymbol):
                if foundSymbol == symbol {
                    return bits


        // Calculate maximum amount of leaves possible in a tree.
        self.leafCount = 1 << (lengths.last![1] + 1)
        self.tree = Array(repeating: .leaf(-1), count: leafCount)

        // Calculates symbols for each length in 'lengths' array and put them in the tree.
        var loopBits = -1
