This function currently recursively duplicates entire trees and is called extensively, needs better memory management (pool/arena?)