refactor union find