Make the algorithm work for forests with differing numbers of layers.