Cleaner solution to earlier rewrite_refs bug