replaceString function
minor refactoring