tree data BUGFIX creating parent chain in dup

Obsolete invalid code updated.
diff --git a/src/tree_data.c b/src/tree_data.c
index d38acf2..778f174 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -1834,10 +1834,11 @@
  * @return LY_ERR value.
  */
 static LY_ERR
-lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, const struct lyd_node_inner *parent,
-        uint32_t options, struct lyd_node **dup_parent, struct lyd_node_inner **local_parent)
+lyd_dup_get_local_parent(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent,
+        uint32_t options, struct lyd_node **dup_parent, struct lyd_node **local_parent)
 {
-    const struct lyd_node_inner *orig_parent, *iter;
+    const struct lyd_node *orig_parent;
+    struct lyd_node *iter;
     ly_bool repeat = 1, ext_parent = 0;
 
     *dup_parent = NULL;
@@ -1846,7 +1847,7 @@
     if (node->flags & LYD_EXT) {
         ext_parent = 1;
     }
-    for (orig_parent = node->parent; repeat && orig_parent; orig_parent = orig_parent->parent) {
+    for (orig_parent = lyd_parent(node); repeat && orig_parent; orig_parent = lyd_parent(orig_parent)) {
         if (ext_parent) {
             /* use the standard context */
             trg_ctx = LYD_CTX(orig_parent);
@@ -1857,32 +1858,23 @@
             repeat = 0;
         } else if (parent && (LYD_CTX(parent) != LYD_CTX(orig_parent)) &&
                 lyd_compare_schema_equal(parent->schema, orig_parent->schema) &&
-                lyd_compare_schema_parents_equal(&parent->node, &orig_parent->node)) {
+                lyd_compare_schema_parents_equal(parent, orig_parent)) {
             iter = parent;
             repeat = 0;
         } else {
             iter = NULL;
-            LY_CHECK_RET(lyd_dup_r((struct lyd_node *)orig_parent, trg_ctx, NULL, 0, (struct lyd_node **)&iter, options,
-                    (struct lyd_node **)&iter));
+            LY_CHECK_RET(lyd_dup_r(orig_parent, trg_ctx, NULL, 0, &iter, options, &iter));
         }
+
         if (!*local_parent) {
-            *local_parent = (struct lyd_node_inner *)iter;
+            /* update local parent (created parent) */
+            *local_parent = iter;
         }
-        if (iter->child) {
-            /* 1) list - add after keys
-             * 2) provided parent with some children */
-            iter->child->prev->next = *dup_parent;
-            if (*dup_parent) {
-                (*dup_parent)->prev = iter->child->prev;
-                iter->child->prev = *dup_parent;
-            }
-        } else {
-            ((struct lyd_node_inner *)iter)->child = *dup_parent;
-        }
+
         if (*dup_parent) {
-            (*dup_parent)->parent = (struct lyd_node_inner *)iter;
+            lyd_insert_node(iter, NULL, *dup_parent, 0);
         }
-        *dup_parent = (struct lyd_node *)iter;
+        *dup_parent = iter;
         if (orig_parent->flags & LYD_EXT) {
             ext_parent = 1;
         }
@@ -1890,8 +1882,8 @@
 
     if (repeat && parent) {
         /* given parent and created parents chain actually do not interconnect */
-        LOGERR(trg_ctx, LY_EINVAL,
-                "Invalid argument parent (%s()) - does not interconnect with the created node's parents chain.", __func__);
+        LOGERR(trg_ctx, LY_EINVAL, "None of the duplicated node \"%s\" schema parents match the provided parent \"%s\".",
+                LYD_NAME(node), LYD_NAME(parent));
         return LY_EINVAL;
     }
 
@@ -1899,14 +1891,14 @@
 }
 
 static LY_ERR
-lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node_inner *parent, uint32_t options,
+lyd_dup(const struct lyd_node *node, const struct ly_ctx *trg_ctx, struct lyd_node *parent, uint32_t options,
         ly_bool nosiblings, struct lyd_node **dup)
 {
     LY_ERR rc;
     const struct lyd_node *orig;          /* original node to be duplicated */
     struct lyd_node *first = NULL;        /* the first duplicated node, this is returned */
     struct lyd_node *top = NULL;          /* the most higher created node */
-    struct lyd_node_inner *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
+    struct lyd_node *local_parent = NULL; /* the direct parent node for the duplicated node(s) */
 
     assert(node && trg_ctx);
 
@@ -1921,7 +1913,7 @@
         if (lysc_is_key(orig->schema)) {
             if (local_parent) {
                 /* the key must already exist in the parent */
-                rc = lyd_find_sibling_schema(local_parent->child, orig->schema, first ? NULL : &first);
+                rc = lyd_find_sibling_schema(lyd_child(local_parent), orig->schema, first ? NULL : &first);
                 LY_CHECK_ERR_GOTO(rc, LOGINT(trg_ctx), error);
             } else {
                 assert(!(options & LYD_DUP_WITH_PARENTS));
@@ -1931,8 +1923,7 @@
             }
         } else {
             /* if there is no local parent, it will be inserted into first */
-            rc = lyd_dup_r(orig, trg_ctx, local_parent ? &local_parent->node : NULL, 0, &first, options,
-                    first ? NULL : &first);
+            rc = lyd_dup_r(orig, trg_ctx, local_parent, 0, &first, options, first ? NULL : &first);
             LY_CHECK_GOTO(rc, error);
         }
         if (nosiblings) {
@@ -1989,7 +1980,7 @@
     LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
     LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
 
-    return lyd_dup(node, LYD_CTX(node), parent, options, 1, dup);
+    return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 1, dup);
 }
 
 LIBYANG_API_DEF LY_ERR
@@ -1998,7 +1989,7 @@
 {
     LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
 
-    return lyd_dup(node, trg_ctx, parent, options, 1, dup);
+    return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 1, dup);
 }
 
 LIBYANG_API_DEF LY_ERR
@@ -2007,7 +1998,7 @@
     LY_CHECK_ARG_RET(NULL, node, LY_EINVAL);
     LY_CHECK_RET(lyd_dup_ctx_check(node, parent));
 
-    return lyd_dup(node, LYD_CTX(node), parent, options, 0, dup);
+    return lyd_dup(node, LYD_CTX(node), (struct lyd_node *)parent, options, 0, dup);
 }
 
 LIBYANG_API_DEF LY_ERR
@@ -2016,7 +2007,7 @@
 {
     LY_CHECK_ARG_RET(trg_ctx, node, trg_ctx, LY_EINVAL);
 
-    return lyd_dup(node, trg_ctx, parent, options, 0, dup);
+    return lyd_dup(node, trg_ctx, (struct lyd_node *)parent, options, 0, dup);
 }
 
 LIBYANG_API_DEF LY_ERR