Rework ref rewriting