Re-factored get_parent_hierarchy_id function