Fix bug in replace for when used on topmost node.