refactoring the visitor to regroup algorithm