rewrite hierarchical merging