Refactored derivation-visiting code