Use a better find and replace function.
Use a better find and replace function (thanks @dracos). Fixes #1