refactored union find