Modify implementation of merge() and replace()