small refactoring + UnionFind