Add references also on parent and root
Improve the query to remove orphans