modified replacement function to use sensible regexes; fixed bugs