Add a rewriteRefs helper function.