Refactoring connected components