Simplify replaceWith method. Closes gh-1276

(cherry picked from commit 642e9a45579cfa90861b8ea71a95dd077775caaf)