Bugfixes for replace method.