refactor node visitor and merge logic