       */
 -    public void exitScope(NodeTraversal t) {
 -      if (t.inGlobalScope()) {
--        return;
--      }
 +    @Override
 +    public void visit(NodeTraversal t, Node node, Node parent) {
 +      if (node.getType() == Token.NAME) {
 +        String oldName = node.getString();
-+        if (oldName.indexOf(ContextualRenamer.UNIQUE_ID_SEPARATOR) != -1) {
++        if (containsSeparator(oldName)) {
 +          Scope scope = t.getScope();
 +          Var var = t.getScope().getVar(oldName);
 +          if (var == null || var.isGlobal()) {
-+            return;
-+          }
+         return;
+       }
  
 -      for (Iterator<Var> it = t.getScope().getVars(); it.hasNext();) {
 -        Var v = it.next();

-            Preconditions.checkState(n.getType() == Token.NAME);
-            n.setString(newName);
-          }
-          compiler.reportCodeChange();
-        }
-        nameMap.remove(name);
-      }
-    }
-
-    @Override
-    public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
-      return true;
-    }
-
-    @Override
-    public void visit(NodeTraversal t, Node node, Node parent) {
-      if (t.inGlobalScope()) {
-        return;
-      }
+              Node parentNode = var.getParentNode();
+              if (parentNode.getType() == Token.FUNCTION &&
+                  parentNode == var.scope.getRootNode()) {

 
     private ContextualRenameInverter(AbstractCompiler compiler) {
       this.compiler = compiler;
@@ -266,105 +263,86 @@ class MakeDeclaredNamesUnique
     private boolean containsSeparator(String name) {
       return name.indexOf(ContextualRenamer.UNIQUE_ID_SEPARATOR) != -1;
     }
+    private static String getOrginalNameInternal(String name, int index) {
+      return name.substring(0, index);
+    }
 
     /**
      * Prepare a set for the new scope.
