tree data REFACTOR minor fixes
diff --git a/src/tree_data.c b/src/tree_data.c
index 9ccf994..f8568aa 100644
--- a/src/tree_data.c
+++ b/src/tree_data.c
@@ -3279,27 +3279,24 @@
 {
     struct ly_ht *ht;
     uint32_t hash;
-    struct lyd_leafref_links_rec rec;
+    struct lyd_leafref_links_rec rec = {0};
 
     assert(node);
     assert(record);
 
+    *record = NULL;
+
     if (!(ly_ctx_get_options(LYD_CTX(node)) & LY_CTX_LEAFREF_LINKING)) {
-        *record = NULL;
         return LY_EDENIED;
     }
 
     rec.node = node;
-    rec.leafref_nodes = NULL;
-    rec.target_node = NULL;
-
     ht = LYD_CTX(node)->leafref_links_ht;
     hash = lyht_hash((const char *)&node, sizeof & node);
     if (lyht_find(ht, &rec, hash, (void **)record) == LY_ENOTFOUND) {
         if (create) {
             LY_CHECK_RET(lyht_insert_no_check(ht, &rec, hash, (void **)record));
         } else {
-            *record = NULL;
             return LY_ENOTFOUND;
         }
     }
@@ -3352,7 +3349,6 @@
     struct lyd_node_term *leafref_node;
     struct lysc_node_leaf *leaf_schema;
     struct lysc_type_leafref *lref;
-    LY_ERR ret;
 
     LY_CHECK_ARG_RET(NULL, tree, LY_EINVAL);
 
@@ -3365,21 +3361,22 @@
             if (elem->schema->nodetype & LYD_NODE_TERM) {
                 leafref_node = (struct lyd_node_term *)elem;
                 leaf_schema = (struct lysc_node_leaf *)elem->schema;
+
                 if (leaf_schema->type->basetype == LY_TYPE_LEAFREF) {
                     lref = (struct lysc_type_leafref *)leaf_schema->type;
                     if (lyplg_type_resolve_leafref(lref, elem, &leafref_node->value, tree, &target, &errmsg)) {
+                        /* leafref target not found */
                         free(errmsg);
-                    } else if (target->schema->nodetype & LYD_NODE_TERM) {
-                        ret = lyd_link_leafref_node((struct lyd_node_term *)target, leafref_node);
-                        if (ret != LY_SUCCESS) {
-                            return ret;
-                        }
+                    } else {
+                        /* leafref target found, link it */
+                        LY_CHECK_RET(lyd_link_leafref_node((struct lyd_node_term *)target, leafref_node));
                     }
                 }
             }
             LYD_TREE_DFS_END(sibling, elem);
         }
     }
+
     return LY_SUCCESS;
 }
 
diff --git a/src/tree_data_free.c b/src/tree_data_free.c
index 100cf49..1b4624d 100644
--- a/src/tree_data_free.c
+++ b/src/tree_data_free.c
@@ -150,13 +150,14 @@
     LY_ARRAY_FOR(rec->leafref_nodes, u) {
         if (lyd_get_or_create_leafref_links_record(rec->leafref_nodes[u], &leafref_rec, 0) == LY_SUCCESS) {
             leafref_rec->target_node = NULL;
-            if ((LY_ARRAY_COUNT(leafref_rec->leafref_nodes) == 0) && (leafref_rec->target_node == NULL)) {
+            if (!LY_ARRAY_COUNT(leafref_rec->leafref_nodes) && !leafref_rec->target_node) {
                 lyd_free_leafref_nodes(rec->leafref_nodes[u]);
             }
         }
     }
     LY_ARRAY_FREE(rec->leafref_nodes);
     rec->leafref_nodes = NULL;
+
     /* remove stored target node */
     if (rec->target_node) {
         lyd_unlink_leafref_node(rec->target_node, rec->node);
@@ -172,7 +173,7 @@
 
     assert(node);
 
-    if (lyd_get_or_create_leafref_links_record(node, &rec, 0) != LY_SUCCESS) {
+    if (lyd_get_or_create_leafref_links_record(node, &rec, 0)) {
         return;
     }
 
@@ -187,6 +188,7 @@
 
 /**
  * @brief Free Data (sub)tree.
+ *
  * @param[in] node Data node to be freed.
  * @param[in] top Recursion flag to unlink the root of the subtree being freed.
  */