Added Andrew's changes for generic string replacement.
Rest of Andrew's changes.