Properly deduplicate nodes pointing to transformed nodes

If two nodes referenced a node that references a replaced node, the middle node would get duplicated in replaceNode. This fixes that problem.