diff --git a/CompilerGeneration.jrag b/CompilerGeneration.jrag
index 737433aa36dceadc5c2847bc4225c722832ed9c8..1e3f6a0f4f0902010cd6ca3b3bffb05cf26c1d30 100644
--- a/CompilerGeneration.jrag
+++ b/CompilerGeneration.jrag
@@ -1,7 +1,7 @@
 /* -*-Java-*- */
 
 /* 
- * Copyright (C) 2007,2008,2009  Anders Nilsson <anders.nilsson@cs.lth.se>
+ * Copyright (C) 2007-2011  Anders Nilsson <anders.nilsson@control.lth.se>
  *
  * This file is part of XMLSchemaCompiler.
  *
@@ -82,7 +82,11 @@ aspect AbsGrammarGeneration {
     void XsElement.genAbsGrammar(PrintStream pStream) {
         if (hasName()) { // If not, it's just a reference to an element declaration
             String name = fixName(name());
-            pStream.println(name + " : ComplexElement ::= ;");
+            pStream.print(name + " : ComplexElement ::= ");
+			// for (Element e : getElements()) {
+			// 	e.genAbsGrammarNTA(pStream);
+			// }
+			pStream.println(";");
         }
     }
 
@@ -390,6 +394,13 @@ aspect Misc {
                     replace("\""," ").trim();
 			}
 		}
+		// Ok, let's check for "ref" instead
+		for (int i=0; i<getNumAttribute(); i++) {
+			if (getAttribute(i) instanceof Ref) {
+				return getAttribute(i).getAttValue().getSTRING_LITERAL().
+                    replace("\""," ").trim();
+			}
+		}
 		// OK, no name found so let's report that
 		return "NoNameFound";
 	}
@@ -413,12 +424,12 @@ aspect Misc {
 	static HashSet<String> ASTNode.registeredNames = new HashSet();
 
 	static String ASTNode.fixName(String s) {
-		if (s.equals("class")) {
-			return "clazz";
-		}
-		if (problemNames.contains(s)) {
+		// if (s.equals("class")) {
+		// 	return "clazz";
+		// }
+		// if (problemNames.contains(s)) {
 			s = "_" + s;
-		}
+		// }
 		s = s.replace("-","_").replace(":","");
 		return s;
 	}
@@ -460,10 +471,63 @@ aspect GenAspects {
 		pStream.println();
 // 		genRewrites(pStream);
 // 		genMisc(pStream);
+		genNTA(pStream);
 		genPrettyPrinter(pStream);
 	}
 }
 
+aspect NTA {
+	void ASTNode.genNTA(PrintStream pStream) {}
+	void Start.genNTA(PrintStream pStream) {
+		pStream.println("aspect NTA {");
+		getSpecification().getXsSchema().genNTA(pStream);
+		pStream.println("}");
+	}
+	void ComplexElement.genNTA(PrintStream pStream) {
+		for (Element e : getElements()) {
+			e.genNTA(pStream);
+		}
+	}
+	void XsElement.genNTA(PrintStream pStream) {
+		for (Element e : getElements()) {
+			e.genNTAAttribute(pStream,this);
+		}		
+	}
+
+	void Element.genNTAAttribute(PrintStream pStream,ComplexElement elem) {}
+	void ComplexElement.genNTAAttribute(PrintStream pStream,ComplexElement elem) {
+		for (Element e : getElements()) {
+			e.genNTAAttribute(pStream,elem);
+		}
+	}
+    void XsAttribute.genNTAAttribute(PrintStream pStream,ComplexElement elem) {
+		pStream.println("    syn nta "+fixName(name())+" "+
+					  fixName(elem.name())+".get"+name()+"() {");
+		pStream.println("        for (Attribute a : getAttributes()) {");
+		pStream.println("            if (a instanceof "+fixName(name())+") {");
+		pStream.println("                return ("+fixName(name())+") a;");
+		pStream.println("            }");
+		pStream.println("        }");
+		pStream.println("        return null;");
+		pStream.println("    }");
+	}
+    void XsElement.genNTAAttribute(PrintStream pStream,ComplexElement elem) {
+		pStream.println("    syn nta List<"+fixName(name())+"> "+
+					  fixName(elem.name())+".get"+name()+"() {");
+		pStream.println("        List<"+fixName(name())+"> l = new List<"+
+						fixName(name())+">();"); 
+		pStream.println("        for (Element e : getElements()) {");
+		pStream.println("            if (e instanceof "+fixName(name())+") {");
+		pStream.println("                l.add(("+fixName(name())+")e);");
+		pStream.println("            }");
+		pStream.println("        }");
+		pStream.println("        return l;");
+		pStream.println("    }");
+	}
+}
+
+
+
 aspect GenPrettyPrinter {
 	public void ASTNode.genPrettyPrinter(PrintStream pStream) {
 		for (int i=0; i<getNumChild(); i++) {
diff --git a/GenCompiler.java b/GenCompiler.java
index 9d8ac13b6e44144606055aff44a8fc8bc04ef852..500afaf36945497e487b06186ddeb759af1d951b 100644
--- a/GenCompiler.java
+++ b/GenCompiler.java
@@ -89,6 +89,11 @@ public class GenCompiler extends Parser {
 			pStream = new PrintStream(new File(fileName));
 			ast.genAspects(pStream);
 
+			// Generate rewrites
+			fileName = dir+"/GeneratedRewrites.jrag";
+			pStream = new PrintStream(new File(fileName));
+			ast.genRewrites(pStream);
+
 		} catch (java.io.FileNotFoundException e) {
 			System.out.println("Could not create file: "+fileName);
 			e.printStackTrace();
diff --git a/RewriteGeneration.jrag b/RewriteGeneration.jrag
new file mode 100644
index 0000000000000000000000000000000000000000..3ea3d55f1ccbfbae39d2d5fd889675e9234f3ce0
--- /dev/null
+++ b/RewriteGeneration.jrag
@@ -0,0 +1,31 @@
+/* -*-Java-*- */
+
+/* 
+ * Copyright (C) 2007-2011  Anders Nilsson <anders.nilsson@control.lth.se>
+ *
+ * This file is part of XMLSchemaCompiler.
+ *
+ * XMLSchemaCompiler is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * XMLSchemaCompiler is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with XMLSchemaCompiler.  If not, see <http://www.gnu.org/licenses/>.
+*/
+
+aspect GenerateRewrites {
+		public void Start.genRewrites(PrintStream pStream) {
+	        pStream.println("/* -*-Java-*- */");
+			pStream.println();
+			pStream.println("// Automatically generated JastAdd rewrites file. ");
+			pStream.println();
+			
+		}
+
+}
\ No newline at end of file
diff --git a/WorkArounds.jrag b/WorkArounds.jrag
index eddb1a0545ea842279df105b8c40f8077a155a5f..187b3e018c4700e82c78fc2cfd89ee9ba808e02b 100644
--- a/WorkArounds.jrag
+++ b/WorkArounds.jrag
@@ -8,8 +8,8 @@
 
 
 aspect Fixes {
-	int ASTNode.numChildren;
-	int ASTNode.numChildren() {return jjtGetNumChildren();}
-	int List.getNumNoTransformChild() {return getNumChildNoTransform();}
-	int Opt.getNumChildNoTransfrom() {return getNumChildNoTransform();}
+	// int ASTNode.numChildren;
+	// int ASTNode.numChildren() {return jjtGetNumChildren();}
+	// int List.getNumNoTransformChild() {return getNumChildNoTransform();}
+	// int Opt.getNumChildNoTransfrom() {return getNumChildNoTransform();}
 }
\ No newline at end of file
diff --git a/tools/jastadd2.jar b/tools/jastadd2.jar
index 8856a41c394d8ba30e293a0e1ceb2814bb1a96bc..986eb3c61a4a8eb33929f73f5a135f5e7eac87c2 100644
Binary files a/tools/jastadd2.jar and b/tools/jastadd2.jar differ