modified deepCopy to prevent infinite loops in cyclic references