more replace operation bug fixes