code refactoring

move a common visitor to a template