tree data BUGFIX correct pointer to duplicated parents
diff --git a/src/tree_data.c b/src/tree_data.c
index 410eebd..cc1ad2e 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -1853,7 +1853,7 @@
         uint32_t options, struct lyd_node **dup_parent, struct lyd_node **local_parent)
 {
     const struct lyd_node *orig_parent;
-    struct lyd_node *iter;
+    struct lyd_node *iter = NULL;
     ly_bool repeat = 1, ext_parent = 0;
 
     *dup_parent = NULL;
@@ -1879,17 +1879,21 @@
         } else {
             iter = NULL;
             LY_CHECK_RET(lyd_dup_r(orig_parent, trg_ctx, NULL, 0, &iter, options, &iter));
+
+            /* insert into the previous duplicated parent */
+            if (*dup_parent) {
+                lyd_insert_node(iter, NULL, *dup_parent, 0);
+            }
+
+            /* update the last duplicated parent */
+            *dup_parent = iter;
         }
 
+        /* set the first parent */
         if (!*local_parent) {
-            /* update local parent (created parent) */
             *local_parent = iter;
         }
 
-        if (*dup_parent) {
-            lyd_insert_node(iter, NULL, *dup_parent, 0);
-        }
-        *dup_parent = iter;
         if (orig_parent->flags & LYD_EXT) {
             ext_parent = 1;
         }
@@ -1902,6 +1906,10 @@
         return LY_EINVAL;
     }
 
+    if (*dup_parent && parent) {
+        /* last insert into a prevously-existing parent */
+        lyd_insert_node(parent, NULL, *dup_parent, 0);
+    }
     return LY_SUCCESS;
 }
 
diff --git a/tests/utests/data/test_tree_data.c b/tests/utests/data/test_tree_data.c
index 462913b..494fdf3 100644
--- a/tests/utests/data/test_tree_data.c
+++ b/tests/utests/data/test_tree_data.c
@@ -337,8 +337,7 @@
 
     data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
     CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
-    assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)((struct lyd_node_inner *)tree1->next)->child)->child, NULL,
-            LYD_DUP_WITH_PARENTS, &tree2));
+    assert_int_equal(LY_SUCCESS, lyd_dup_single(lyd_child(lyd_child(tree1->next)), NULL, LYD_DUP_WITH_PARENTS, &tree2));
     int unsigned flag = LYS_CONFIG_R | LYS_SET_ENUM;
 
     CHECK_LYSC_NODE(tree2->schema, NULL, 0, flag, 1, "x", 1, LYS_LEAF, 1, 0, NULL, 0);
@@ -360,8 +359,8 @@
     data = "<l2 xmlns=\"urn:tests:a\"><c><x>b</x></c></l2>";
     CHECK_PARSE_LYD(data, 0, LYD_VALIDATE_PRESENT, tree1);
     assert_int_equal(LY_SUCCESS, lyd_dup_single(tree1->next, NULL, 0, &tree2));
-    assert_int_equal(LY_SUCCESS, lyd_dup_single(((struct lyd_node_inner *)((struct lyd_node_inner *)tree1->next)->child)->child,
-            (struct lyd_node_inner *)tree2, LYD_DUP_WITH_PARENTS, NULL));
+    assert_int_equal(LY_SUCCESS, lyd_dup_single(lyd_child(lyd_child(tree1->next)), (struct lyd_node_inner *)tree2,
+            LYD_DUP_WITH_PARENTS, NULL));
     assert_int_equal(LY_SUCCESS, lyd_compare_single(tree1->next, tree2, LYD_COMPARE_FULL_RECURSION));
     lyd_free_all(tree1);
     lyd_free_all(tree2);