Made util.rewriteIDs robust against circular references