Refactor hierarchical clustering.