Tweaked and commented replacement logic
Tweaked comments