Efficiency fix for basic union find and refactored code.