Refactored common code to use visitorish pattern.