Improved replace, to allow for custom fallbacks.