- cosmas classrefcheck
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/AbstractSyntaxTree.java b/src/main/java/de/ids_mannheim/korap/query/serialize/AbstractSyntaxTree.java
index d0e9ec0..4ad6a01 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/AbstractSyntaxTree.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/AbstractSyntaxTree.java
@@ -56,12 +56,14 @@
 	ArrayList<String> errorMsgs = new ArrayList<String>();
 	ArrayList<String> warnings = new ArrayList<String>();
 	ArrayList<String> announcements = new ArrayList<String>();
+	LinkedHashMap<String, Object> collection = new LinkedHashMap<String,Object>();
 	
 	AbstractSyntaxTree() {
 		requestMap.put("@context", "http://ids-mannheim.de/ns/KorAP/json-ld/v0.2/context.jsonld");
 		requestMap.put("errors", errorMsgs);
 		requestMap.put("warnings", warnings);
 		requestMap.put("announcements", announcements);
+		requestMap.put("collection", collection);
 	}
 	
 	public Map<String, Object> getRequestMap() {
@@ -149,42 +151,44 @@
 		group.put("@type", "korap:group");
 		group.put("operation", "operation:position");
 		group.put("frames", Arrays.asList(allowedFrames));
-		group.put("sharedClasses", Arrays.asList(sharedClasses));
+//		group.put("sharedClasses", Arrays.asList(sharedClasses));
 		group.put("operands", new ArrayList<Object>());
 		// DEPRECATED 'frame'
+		if (sharedClasses.length==0) sharedClasses = new String[]{"classRefCheck:includes"};
 		String frame = "";
-		if (allowedFrames.length==0 && sharedClasses[0]=="sharedClasses:includes") {
+		
+		if (allowedFrames.length==0 && sharedClasses[0]=="classRefCheck:includes") {
 			frame = "frame:contains";
-		} else if (allowedFrames.length==0 && sharedClasses[0]=="sharedClasses:intersects") {
+		} else if (allowedFrames.length==0 && sharedClasses[0]=="classRefCheck:intersects") {
 			frame = "frame:overlaps";
-		} else if (allowedFrames[0]=="frame:startswith" && sharedClasses[0]=="sharedClasses:includes") {
+		} else if (allowedFrames[0]=="frames:startswith" && sharedClasses[0]=="classRefCheck:includes") {
 			frame = "frame:startswith";
-		} else if (allowedFrames[0]=="frame:endswith" && sharedClasses[0]=="sharedClasses:includes") {
+		} else if (allowedFrames[0]=="frames:endswith" && sharedClasses[0]=="classRefCheck:includes") {
 			frame = "frame:endswith";
-		} else if (allowedFrames[0]=="frame:matches" && sharedClasses[0]=="sharedClasses:includes" && sharedClasses.length==1) {
+		} else if (allowedFrames[0]=="frames:matches" && sharedClasses[0]=="classRefCheck:includes" && sharedClasses.length==1) {
 			frame = "frame:endswith";
-		} else if (allowedFrames[0]=="frame:matches" && sharedClasses[0]=="sharedClasses:includes" && sharedClasses[1]=="sharedClasses:unequals") {
+		} else if (allowedFrames[0]=="frames:matches" && sharedClasses[0]=="classRefCheck:includes" && sharedClasses[1]=="classRefCheck:unequals") {
 			frame = "frame:matches";
-		} else if (allowedFrames[0]=="frame:matches" && sharedClasses[0]=="sharedClasses:equals") {
+		} else if (allowedFrames[0]=="frames:matches" && sharedClasses[0]=="classRefCheck:equals") {
 			frame = "frame:matches";			
-		} else if (allowedFrames[0]=="frame:contains" && sharedClasses[0]=="sharedClasses:includes") {
+		} else if (allowedFrames[0]=="frames:contains" && sharedClasses[0]=="classRefCheck:includes") {
 			frame = "frame:contains";
-		} else if (allowedFrames[0]=="frame:startswith" && sharedClasses[0]=="sharedClasses:intersects") {
+		} else if (allowedFrames[0]=="frames:startswith" && sharedClasses[0]=="classRefCheck:intersects") {
 			frame = "frame:overlapsLeft";
-		} else if (allowedFrames[0]=="frame:endswith" && sharedClasses[0]=="sharedClasses:intersects") {
+		} else if (allowedFrames[0]=="frames:endswith" && sharedClasses[0]=="classRefCheck:intersects") {
 			frame = "frame:overlapsRight";
-		} else if (allowedFrames[0]=="frame:matches" && sharedClasses[0]=="sharedClasses:intersects") {
+		} else if (allowedFrames[0]=="frames:matches" && sharedClasses[0]=="classRefCheck:intersects") {
 			frame = "frame:matches";
-		} else if (allowedFrames[0]=="frame:matches" && sharedClasses[0]=="sharedClasses:unequals") {
+		} else if (allowedFrames[0]=="frames:matches" && sharedClasses[0]=="classRefCheck:unequals") {
 			frame = "frame:matches";
-		} else if (allowedFrames[0]=="frame:matches" && sharedClasses[0]=="sharedClasses:equals") {
+		} else if (allowedFrames[0]=="frames:matches" && sharedClasses[0]=="classRefCheck:equals") {
 			frame = "frame:matches";
-		} else if (allowedFrames[0]=="frame:contains" && sharedClasses[0]=="sharedClasses:intersects") {
+		} else if (allowedFrames[0]=="frames:contains" && sharedClasses[0]=="classRefCheck:intersects") {
 			frame = "frame:contains";
 		}
 		group.put("frame", frame);
 		announcements.add("Deprecated 2014-09-22: 'frame' only to be supported until 3 months from deprecation date. " +
-				"Position frames are now expressed through 'frames' and 'sharedClasses'");
+				"Position frames are now expressed through 'frames' and 'sharedClasses'.");
 		return group;
 	}
 	
@@ -198,11 +202,41 @@
 		group.put("operation", "operation:class");
 		if (setBySystem) {
 			group.put("class", 1024+classCount);
+			group.put("classOut", 1024+classCount);
 			announcements.add("A class has been introduced into the backend representation of " +
-					"your query for later reference to a part of the query. The class id is "+classCount);
+					"your query for later reference to a part of the query. The class id is "+(1024+classCount));
 		} else {
 			group.put("class", classCount);
+			group.put("classOut", classCount);
 		}
+		announcements.add("Deprecated 2014-10-07: 'class' only to be supported until 3 months from deprecation date. " +
+				"Classes are now defined using the 'classOut' attribute.");
+		group.put("operands", new ArrayList<Object>());
+		return group;
+	}
+	
+	protected LinkedHashMap<String, Object> makeClassRefCheck(ArrayList<String> check, Integer[] classIn, int classOut) {
+		LinkedHashMap<String, Object> group = new LinkedHashMap<String, Object>();
+		group.put("@type", "korap:group");
+		group.put("operation", "operation:class");
+		if (check.size()==1) {
+			group.put("classRefCheck", check.get(0));
+		} else {
+			group.put("classRefCheck", check);
+		}
+		group.put("classIn", Arrays.asList(classIn));
+		group.put("classOut", classOut);
+		group.put("operands", new ArrayList<Object>());
+		return group;
+	}
+	
+	protected LinkedHashMap<String, Object> makeClassRefOp(String operation, Integer[] classIn, int classOut) {
+		LinkedHashMap<String, Object> group = new LinkedHashMap<String, Object>();
+		group.put("@type", "korap:group");
+		group.put("operation", "operation:class");
+		group.put("classRefOp", operation);
+		group.put("classIn", Arrays.asList(classIn));
+		group.put("classOut", classOut);
 		group.put("operands", new ArrayList<Object>());
 		return group;
 	}
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/CosmasTree.java b/src/main/java/de/ids_mannheim/korap/query/serialize/CosmasTree.java
index ba42dbe..c89f418 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/CosmasTree.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/CosmasTree.java
@@ -24,6 +24,7 @@
  * Map representation of CosmasII syntax tree as returned by ANTLR
  *
  * @author bingel
+ * @version 0.2
  */
 public class CosmasTree extends Antlr3AbstractSyntaxTree {
 
@@ -47,6 +48,12 @@
      */
     boolean inMeta = false;
     /**
+     * If true, a sequence (OPPROX node) is governed by an OPALL node (ALL()-operator), which requires to match
+     * all tokens of the sequence.
+     */
+    boolean inOPALL = false;
+    boolean inOPNHIT = false;
+    /**
      *
      */
     int classCounter = 1;
@@ -76,7 +83,7 @@
      */
     private LinkedList<Tree> sequencedNodes = new LinkedList<Tree>();
 
-    private boolean hasSequentiableSiblings;
+    private boolean nodeHasSequentiableSiblings;
 
     /**
      * Keeps track of operands lists that are to be serialised in an inverted
@@ -91,7 +98,6 @@
     public CosmasTree(String query) throws QueryException {
         this.query = query;
         process(query);
-        System.out.println("\n" + requestMap.get("query"));
         log.info(">>> " + requestMap.get("query") + " <<<");
     }
 
@@ -144,14 +150,14 @@
             if (parent.getChildCount() > 1) {
                 // if node is first child of parent...
                 if (node == parent.getChild(0)) {
-                    hasSequentiableSiblings = false;
+                    nodeHasSequentiableSiblings = false;
                     for (int i = 1; i < parent.getChildCount(); i++) {
                         if (sequentiableCats.contains(getNodeCat(parent.getChild(i)))) {
-                            hasSequentiableSiblings = true;
+                            nodeHasSequentiableSiblings = true;
                             continue;
                         }
                     }
-                    if (hasSequentiableSiblings) {
+                    if (nodeHasSequentiableSiblings) {
                         // Step I: create sequence
                         LinkedHashMap<String, Object> sequence = new LinkedHashMap<String, Object>();
                         sequence.put("@type", "korap:group");
@@ -413,6 +419,7 @@
             
             boolean putIntoOverlapDisjunction = false;
 
+            int min = 0, max = 0;
             // possibly several distance constraints
             for (int i = 0; i < dist_list.getChildCount(); i++) {
                 String direction = dist_list.getChild(i).getChild(0).getChild(0).toStringTree().toLowerCase();
@@ -422,8 +429,8 @@
                 if (minStr.equals("VAL0")) {
                     minStr = "0";
                 }
-                int min = Integer.parseInt(minStr);
-                int max = Integer.parseInt(maxStr);
+                min = Integer.parseInt(minStr);
+                max = Integer.parseInt(maxStr);
                 // If zero word-distance, wrap this sequence in a disjunction along with an overlap position
                 // between the two operands
      /*   
@@ -442,7 +449,9 @@
                 if (exclusion) {
                     distance.put("exclude", true);
                 }
-                constraints.add(distance);
+//                if (! openNodeCats.get(1).equals("OPNHIT")) {
+                	constraints.add(distance);
+//                }
                 if (i==0) {
                 	if (direction.equals("plus")) {
                         inOrder = true;
@@ -457,10 +466,15 @@
             group.put("inOrder", inOrder);
             LinkedHashMap<String, Object> embeddedSequence = group;
             
-            if (! (openNodeCats.get(1).equals("OPBEG") || openNodeCats.get(1).equals("OPEND") || openNodeCats.get(1).equals("OPALL") || openNodeCats.get(1).equals("OPNHIT"))) {
+            if (! (openNodeCats.get(1).equals("OPBEG") || openNodeCats.get(1).equals("OPEND") || inOPALL || openNodeCats.get(1).equals("OPNHIT"))) {
                 wrapOperandInClass(node,1,classCounter);
                 wrapOperandInClass(node,2,classCounter);
                 group = wrapInReference(group, 1024+classCounter++);
+            } else if (openNodeCats.get(1).equals("OPNHIT")) {
+            	LinkedHashMap<String,Object> repetition = makeRepetition(min, max);
+            	((ArrayList<Object>) repetition.get("operands")).add(makeToken());
+            	// TODO go on with this: put the repetition into a class and put it in between the operands
+            	// -> what if there's several distance constraints. with different keys, like /w4,s0? 
             }
             
             LinkedHashMap<String,Object> sequence = null;
@@ -499,22 +513,42 @@
         // inlcusion or overlap
         if (nodeCat.equals("OPIN") || nodeCat.equals("OPOV")) {
             // Step I: create group
-        	wrapOperandInClass(node,1,classCounter);
+        	wrapOperandInClass(node,2,classCounter++);
+        	wrapOperandInClass(node,1,classCounter++);
 //            LinkedHashMap<String, Object> posgroup = makePosition(null);
             LinkedHashMap<String, Object> posgroup = makeGroup("position");
+            LinkedHashMap<String, Object> positionOptions;
 //            posgroup
             if (nodeCat.equals("OPIN")) {
-                posgroup = parseOPINOptions(node, posgroup);
+            	positionOptions = parseOPINOptions(node);
             } else {
-            	posgroup = parseOPOVOptions(node, posgroup);
+            	positionOptions = parseOPOVOptions(node);
+            }
+            posgroup.put("frames", positionOptions.get("frames"));
+            posgroup.put("frame", positionOptions.get("frame"));
+            if (positionOptions.containsKey("exclude")) {
+            	posgroup.put("exclude", positionOptions.get("exclude"));
+            }
+            if (positionOptions.containsKey("grouping")) {
+            	posgroup.put("grouping", positionOptions.get("grouping"));
             }
             objectStack.push(posgroup);
             // mark this an inverted operands object
             invertedOperandsLists.push((ArrayList<Object>) posgroup.get("operands"));
             stackedObjects++;
-            // Step II: wrap in reference (limit match to first argument) and decide where to put
-            LinkedHashMap<String, Object> submatchgroup = wrapInReference(posgroup, 1024+classCounter);
-            putIntoSuperObject(submatchgroup, 1);
+            // Step II: wrap in reference and decide where to put
+            ArrayList<String> check = (ArrayList<String>) positionOptions.get("classRefCheck");
+            Integer[] classIn = new Integer[]{1024+classCounter-2,1024+classCounter-1};
+            LinkedHashMap<String, Object> classRefCheck = makeClassRefCheck(check, classIn, 1024+classCounter);
+            ((ArrayList<Object>) classRefCheck.get("operands")).add(posgroup);
+            LinkedHashMap<String, Object> focusGroup = null;
+            if ((boolean) positionOptions.get("matchall") == true) {
+            	focusGroup = makeResetReference();
+            	((ArrayList<Object>) focusGroup.get("operands")).add(classRefCheck);
+            } else { // match only first argument
+            	focusGroup = wrapInReference(classRefCheck, 1024+classCounter-1);
+            }
+            putIntoSuperObject(focusGroup, 1);
         }
 
         // Wrap the argument of an #IN operator in a previously defined container
@@ -536,17 +570,22 @@
 //        	}
         }
 
+        if (nodeCat.equals("OPALL")) {
+        	inOPALL = true;
+        }
+        
         if (nodeCat.equals("OPNHIT")) {
-            ArrayList<Integer> classRef = new ArrayList<Integer>();
-            classRef.add(1024+classCounter);
+            Integer[] classRef = new Integer[]{1024+classCounter+1, 1024+classCounter+2}; 
 //            classRef.add(classCounter + 1);  // yes, do this twice (two classes)!
-            LinkedHashMap<String, Object> group = makeReference(classRef);
-            group.put("classRefOp", "classRefOp:inversion");
+            LinkedHashMap<String, Object> group = makeReference(1024+classCounter);
+            LinkedHashMap<String, Object> classRefCheck = makeClassRefOp("classRefOp:inversion", classRef, classCounter+1024);
             ArrayList<Object> operands = new ArrayList<Object>();
+            operands.add(classRefCheck);
             group.put("operands", operands);
-            wrapOperandInClass(node.getChild(0),1,1024+classCounter); // direct child is OPPROX
-            wrapOperandInClass(node.getChild(0),2,1024+classCounter++);
-            objectStack.push(group);
+            classCounter++;
+            wrapOperandInClass(node.getChild(0),1,classCounter++); // direct child is OPPROX
+            wrapOperandInClass(node.getChild(0),2,classCounter++);
+            objectStack.push(classRefCheck);
             stackedObjects++;
             putIntoSuperObject(group, 1);
         }
@@ -592,7 +631,7 @@
 
                 // create the group expressing the position constraint
                 String[] frames = new String[]{c.position};
-                String[] sharedClasses = new String[]{"sharedClasses:includes"};  // OPBED only defines #IN-corresponding positions
+                String[] sharedClasses = new String[]{};  // OPBED only defines frame constraint, neglects intersection type
             	LinkedHashMap<String,Object> posgroup = makePosition(frames, sharedClasses);
 //                LinkedHashMap<String, Object> posgroup = makePosition(c.position);
                 ArrayList<Object> operands = (ArrayList<Object>) posgroup.get("operands");
@@ -617,7 +656,7 @@
             } else {
                 // node has several conditions (like 'sa, -pa')
                 // -> create identity position group and embed all position groups there
-                LinkedHashMap<String, Object> conjunct = makePosition(new String[]{"frame:matches"}, new String[]{"sharedClasses:equals"});
+                LinkedHashMap<String, Object> conjunct = makePosition(new String[]{"frames:matches"}, new String[]{"classRefCheck:equals"});
 //                ArrayList<Object> distances = new ArrayList<Object>();
 //                distances.add(makeDistance("w", 0,0));
 //                conjunct.put("distances", distances);
@@ -630,7 +669,7 @@
                     // make position group
                     CosmasCondition c = new CosmasCondition(conditions.getChild(i));
                     String[] frames = new String[]{c.position};
-                    String[] sharedClasses = new String[]{"sharedClasses:includes"};  // OPBED only defines #IN-corresponding positions
+                    String[] sharedClasses = new String[]{};  // OPBED only defines frame constraint, neglects intersection type
                 	LinkedHashMap<String,Object> posGroup = makePosition(frames, sharedClasses);
                     operands.add(posGroup);
                     if (c.negated) posGroup.put("exclude", "true");
@@ -697,6 +736,11 @@
         if (nodeCat.equals("ARG2") && openNodeCats.get(1).equals("OPNOT")) {
             negate = false;
         }
+        
+        if (nodeCat.equals("OPALL")) {
+        	inOPALL = false;
+        }
+        
         openNodeCats.pop();
     }
 
@@ -743,59 +787,60 @@
     }
 
     @SuppressWarnings("unchecked")
-	private LinkedHashMap<String, Object> parseOPINOptions(Tree node, LinkedHashMap<String, Object> posgroup) {
+	private LinkedHashMap<String, Object> parseOPINOptions(Tree node) {
         Tree posnode = getFirstChildWithCat(node, "POS");
         Tree rangenode = getFirstChildWithCat(node, "RANGE");
         Tree exclnode = getFirstChildWithCat(node, "EXCL");
         Tree groupnode = getFirstChildWithCat(node, "GROUP");
         boolean negatePosition = false;
-
+        LinkedHashMap<String, Object> posOptions = new LinkedHashMap<String, Object>();
         ArrayList<String> positions = new ArrayList<String>();
-        ArrayList<String> sharedClasses = new ArrayList<String>();
+        ArrayList<String> classRefCheck = new ArrayList<String>();
+        posOptions.put("matchall", false);
         String frame = "";
         String posOption = null; 
         if (posnode != null) {
             posOption = posnode.getChild(0).toStringTree();
             switch (posOption) {
             case "L":
-                positions.add("frame:startswith");
-                sharedClasses.add("sharedClasses:includes");
+                positions.add("frames:startswith");
+                classRefCheck.add("classRefCheck:includes");
                 frame = "startswith";
                 break;
             case "R":
-            	positions.add("frame:endswith");
-                sharedClasses.add("sharedClasses:includes");
+            	positions.add("frames:endswith");
+                classRefCheck.add("classRefCheck:includes");
                 frame = "endswith";
                 break;
             case "F":
-            	positions.add("frame:matches");
-                sharedClasses.add("sharedClasses:includes");
+            	positions.add("frames:matches");
+                classRefCheck.add("classRefCheck:includes");
                 frame = "matches";
                 break;
             case "FE":
-            	positions.add("frame:matches");
-                sharedClasses.add("sharedClasses:equals");
+            	positions.add("frames:matches");
+                classRefCheck.add("classRefCheck:equals");
                 frame = "matches";
                 break;
             case "FI":
-            	positions.add("frame:matches");
-            	sharedClasses.add("sharedClasses:unequals");
-                sharedClasses.add("sharedClasses:includes");
+            	positions.add("frames:matches");
+            	classRefCheck.add("classRefCheck:unequals");
+                classRefCheck.add("classRefCheck:includes");
                 frame = "matches-noident";
                 break;
             case "N": 
-            	positions.add("frame:contains");
-                sharedClasses.add("sharedClasses:includes");
+            	positions.add("frames:contains");
+                classRefCheck.add("classRefCheck:includes");
                 frame = "contains";
                 break;
             }
         } else {
-        	sharedClasses.add("sharedClasses:includes");
+        	classRefCheck.add("classRefCheck:includes");
         	frame = "contains";
         }
-        posgroup.put("frames", positions);
-        posgroup.put("sharedClasses", sharedClasses);
-        posgroup.put("frame", "frame:"+frame);
+        posOptions.put("frames", positions);
+        posOptions.put("classRefCheck", classRefCheck);
+        posOptions.put("frame", "frame:"+frame);
         announcements.add("Deprecated 2014-09-22: 'frame' only to be supported until 3 months from deprecation date. " +
 				"Position frames are now expressed through 'frames' and 'sharedClasses'");
         
@@ -808,92 +853,108 @@
         if (rangenode != null) {
             String range = rangenode.getChild(0).toStringTree().toLowerCase();
             if (range.equals("all")) {
-            	LinkedHashMap<String,Object> ref = makeResetReference(); // reset all defined classes
-            	wrapOperand(node,2,ref);
+            	posOptions.put("matchall", true);
+//            	LinkedHashMap<String,Object> ref = makeResetReference(); // reset all defined classes
+//            	wrapOperand(node,2,ref);
             }
         }
 
         if (negatePosition) {
-            posgroup.put("exclude", "true");
+        	posOptions.put("exclude", "true");
         }
 
         if (groupnode != null) {
             String grouping = groupnode.getChild(0).toStringTree().equals("max") ? "true" : "false";
-            posgroup.put("grouping", grouping);
+            posOptions.put("grouping", grouping);
         }
-        return posgroup;
+        return posOptions;
     }
 
   
 
-	private LinkedHashMap<String, Object> parseOPOVOptions(Tree node, LinkedHashMap<String, Object> posgroup) {
+	private LinkedHashMap<String, Object> parseOPOVOptions(Tree node) {
     	boolean negatePosition = false;
         Tree posnode = getFirstChildWithCat(node, "POS");
+        Tree rangenode = getFirstChildWithCat(node, "RANGE");
         Tree exclnode = getFirstChildWithCat(node, "EXCL");
         Tree groupnode = getFirstChildWithCat(node, "GROUP");
-
+        LinkedHashMap<String, Object> posOptions = new LinkedHashMap<String, Object>();
         ArrayList<String> positions = new ArrayList<String>();
-        ArrayList<String> sharedClasses = new ArrayList<String>();
+        ArrayList<String> classRefCheck = new ArrayList<String>();
+        posOptions.put("matchall", false);
         String frame = "";
         String posOption = null; 
         if (posnode != null) {
             posOption = posnode.getChild(0).toStringTree();
             switch (posOption) {
             case "L":
-                positions.add("frame:startswith");
-                positions.add("frame:overlapsLeft");
-                sharedClasses.add("sharedClasses:intersects");
+                positions.add("frames:startswith");
+                positions.add("frames:overlapsLeft");
+                classRefCheck.add("classRefCheck:intersects");
                 frame = "overlapsLeft";
                 break;
             case "R":
-            	positions.add("frame:endswith");
-            	positions.add("frame:overlapsRight");
-                sharedClasses.add("sharedClasses:intersects");
+            	positions.add("frames:endswith");
+            	positions.add("frames:overlapsRight");
+                classRefCheck.add("classRefCheck:intersects");
                 frame = "overlapsRight";
                 break;
             case "F":
-            	positions.add("frame:matches");
-                sharedClasses.add("sharedClasses:intersects");
+            	positions.add("frames:matches");
+                classRefCheck.add("classRefCheck:intersects");
                 frame = "matches";
                 break;
             case "FE":
-            	positions.add("frame:matches");
-                sharedClasses.add("sharedClasses:equals");
+            	positions.add("frames:matches");
+                classRefCheck.add("classRefCheck:equals");
                 frame = "matches";
                 break;
             case "FI":
-            	positions.add("frame:matches");
-            	sharedClasses.add("sharedClasses:unequals");
+            	positions.add("frames:matches");
+            	classRefCheck.add("classRefCheck:unequals");
             	frame = "matches-noident";
                 break;
             case "X": 
-            	positions.add("frame:contains");
-                sharedClasses.add("sharedClasses:intersects");
+            	positions.add("frames:contains");
+                classRefCheck.add("classRefCheck:intersects");
                 frame = "overlaps";
                 break;
             }
         } else {
-        	sharedClasses.add("sharedClasses:intersects");
+        	classRefCheck.add("classRefCheck:intersects");
         	frame = "overlaps";
         }
         
-        posgroup.put("frames", positions);
-        posgroup.put("sharedClasses", sharedClasses);
-        posgroup.put("frame", "frame:"+frame);
+        posOptions.put("frames", positions);
+        posOptions.put("classRefCheck", classRefCheck);
+        posOptions.put("frame", "frame:"+frame);
         announcements.add("Deprecated 2014-09-22: 'frame' only to be supported until 3 months from deprecation date. " +
 				"Position frames are now expressed through 'frames' and 'sharedClasses'");
 
         if (exclnode != null) {
             if (exclnode.getChild(0).toStringTree().equals("YES")) {
-                posgroup.put("match", "match:" + "ne");
+                negatePosition = !negatePosition;
             }
         }
         
-        if (groupnode != null) {
-            String grouping = groupnode.getChild(0).toStringTree().equals("@max") ? "true" : "false";
-            posgroup.put("grouping", grouping);
+        if (rangenode != null) {
+            String range = rangenode.getChild(0).toStringTree().toLowerCase();
+            if (range.equals("all")) {
+            	posOptions.put("matchall", true);
+//            	LinkedHashMap<String,Object> ref = makeResetReference(); // reset all defined classes
+//            	wrapOperand(node,2,ref);
+            }
         }
-        return posgroup;
+
+        if (negatePosition) {
+        	posOptions.put("exclude", "true");
+        }
+
+        if (groupnode != null) {
+            String grouping = groupnode.getChild(0).toStringTree().equals("max") ? "true" : "false";
+            posOptions.put("grouping", grouping);
+        }
+        return posOptions;
     }
 
     @SuppressWarnings({ "unchecked" })
@@ -986,7 +1047,6 @@
     		ArrayList<Object> topObjectOperands = (ArrayList<Object>) objectStack.get(objStackPosition).get("operands");
     		if (!invertedOperandsLists.contains(topObjectOperands)) {
     			topObjectOperands.add(object);
-    			System.out.println(objectStack.get(objStackPosition));
     		} else {
     			topObjectOperands.add(0, object);
     		}
@@ -1070,10 +1130,13 @@
 //                "wegen #OV(F) <s>"
 //        		"Sonne /s0 Mond",
         		"Sonne /+w1:4 Mond /-w1:7 Sterne",
-        		"Der:t",
-        		"&mond-"
+        		"Der:ta",
+        		"&mond-",
+        		"gehen /+w10 voran %w10 Beispiel",
+        		"(gehen /+w10 voran) %w10 Beispiel",
+        		"#BED(der Mann , sa,-pa)"
         };
-		CosmasTree.verbose=true;
+//		CosmasTree.verbose=true;
         for (String q : queries) {
             try {
                 System.out.println(q);
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/TreeTemplate.java b/src/main/java/de/ids_mannheim/korap/query/serialize/TreeTemplate.java
index 80d4a12..7030f5c 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/TreeTemplate.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/TreeTemplate.java
@@ -2,13 +2,9 @@
 
 import java.lang.reflect.Method;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
-import java.util.Map;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
 
 import org.antlr.v4.runtime.ANTLRInputStream;
 import org.antlr.v4.runtime.BailErrorStrategy;
@@ -21,20 +17,15 @@
 
 import de.ids_mannheim.korap.query.annis.AqlLexer;
 import de.ids_mannheim.korap.query.annis.AqlParser;
-import de.ids_mannheim.korap.query.serialize.AbstractSyntaxTree;
 import de.ids_mannheim.korap.util.QueryException;
 
 /**
  * Map representation of syntax tree as returned by ANTLR
- * @author joachim
+ * @author bingel
  *
  */
 public class TreeTemplate extends Antlr4AbstractSyntaxTree {
 	/**
-	 * Top-level map representing the whole request.
-	 */
-	LinkedHashMap<String,Object> requestMap = new LinkedHashMap<String,Object>();
-	/**
 	 * Keeps track of open node categories
 	 */
 	LinkedList<String> openNodeCats = new LinkedList<String>();
@@ -74,51 +65,16 @@
 	 * @param parser The ANTLR parser instance that generated the parse tree
 	 */
 	public TreeTemplate(String query) {
-//		prepareContext();
-		requestMap.put("@context", "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld");
 		try {
 			process(query);
 		} catch (QueryException e) {
 			e.printStackTrace();
 		}
-		System.out.println(">>> "+requestMap.get("query")+" <<<");
-	}
-
-	private void prepareContext() {
-		LinkedHashMap<String,Object> context = new LinkedHashMap<String,Object>();
-		LinkedHashMap<String,Object> operands = new LinkedHashMap<String,Object>();
-		LinkedHashMap<String,Object> relation = new LinkedHashMap<String,Object>();
-		LinkedHashMap<String,Object> classMap = new LinkedHashMap<String,Object>();
-		
-		operands.put("@id", "korap:operands");
-		operands.put("@container", "@list");
-		
-		relation.put("@id", "korap:relation");
-		relation.put("@type", "korap:relation#types");
-		
-		classMap.put("@id", "korap:class");
-		classMap.put("@type", "xsd:integer");
-		
-		context.put("korap", "http://korap.ids-mannheim.de/ns/query");
-		context.put("@language", "de");
-		context.put("operands", operands);
-		context.put("relation", relation);
-		context.put("class", classMap);
-		context.put("query", "korap:query");
-		context.put("filter", "korap:filter");
-		context.put("meta", "korap:meta");
-		
-		requestMap.put("@context", context);		
-	}
-
-	@Override
-	public Map<String, Object> getRequestMap() {
-		return requestMap;
 	}
 	
 	@Override
 	public void process(String query) throws QueryException {
-		ParseTree tree = parseAnnisQuery(query);
+		ParseTree tree = parseQuery(query);
 		System.out.println("Processing Annis QL");
 		processNode(tree);
 	}
@@ -198,16 +154,16 @@
 		}
 	}
 	
-	private static ParserRuleContext parseAnnisQuery (String p) throws QueryException {
-		Lexer poliqarpLexer = new AqlLexer((CharStream)null);
+	private static ParserRuleContext parseQuery (String q) throws QueryException {
+		Lexer qlLexer = new AqlLexer((CharStream)null);
 	    ParserRuleContext tree = null;
 	    // Like p. 111
 	    try {
 
 	      // Tokenize input data
-	      ANTLRInputStream input = new ANTLRInputStream(p);
-	      poliqarpLexer.setInputStream(input);
-	      CommonTokenStream tokens = new CommonTokenStream(poliqarpLexer);
+	      ANTLRInputStream input = new ANTLRInputStream(q);
+	      qlLexer.setInputStream(input);
+	      CommonTokenStream tokens = new CommonTokenStream(qlLexer);
 	      qlParser = new AqlParser(tokens);
 
 	      // Don't throw out erroneous stuff
@@ -231,27 +187,4 @@
 	    // Return the generated tree
 	    return tree;
 	  }
-	
-	public static void main(String[] args) {
-		/*
-		 * For testing
-		 */
-		String[] queries = new String[] {
-			};
-		TreeTemplate.verbose=true;
-		for (String q : queries) {
-			try {
-				System.out.println(q);
-				System.out.println(TreeTemplate.parseAnnisQuery(q).toStringTree(TreeTemplate.qlParser));
-				@SuppressWarnings("unused")
-				TreeTemplate at = new TreeTemplate(q);
-//				System.out.println(TreeTemplate.parseAnnisQuery(q).toStringTree(TreeTemplate.aqlParser));
-				System.out.println();
-				
-			} catch (NullPointerException | QueryException npe) {
-				npe.printStackTrace();
-			}
-		}
-	}
-
 }
\ No newline at end of file
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/util/CosmasCondition.java b/src/main/java/de/ids_mannheim/korap/query/serialize/util/CosmasCondition.java
index 86428f8..454e9f6 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/util/CosmasCondition.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/util/CosmasCondition.java
@@ -20,6 +20,6 @@
 		elem = nodeString.substring(0, 1);
 		nodeString = nodeString.substring(1);
 		
-		position = nodeString.equals("a") ? "frame:startswith" : "frame:endswith";
+		position = nodeString.equals("a") ? "frames:startswith" : "frames:endswith";
 	}
 }
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/util/ResourceMapper.java b/src/main/java/de/ids_mannheim/korap/query/serialize/util/ResourceMapper.java
index 11a805b..661976d 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/util/ResourceMapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/util/ResourceMapper.java
@@ -18,7 +18,7 @@
         map.put("const", "c");
         map.put("dep", "d");
         map.put("morph", "msd");
-        map.put("ANA", "pos");
+        map.put("ANA", "p");
     }
 
     public static String descriptor2policy(String descriptor) {
diff --git a/src/test/java/CosmasTreeTest.java b/src/test/java/CosmasTreeTest.java
index 980de39..e074ee8 100644
--- a/src/test/java/CosmasTreeTest.java
+++ b/src/test/java/CosmasTreeTest.java
@@ -14,7 +14,7 @@
 	
 	@Test
 	public void testContext() throws QueryException {
-		String contextString = "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld";
+		String contextString = "http://ids-mannheim.de/ns/KorAP/json-ld/v0.2/context.jsonld";
 		ct = new CosmasTree("Test");
 		assertEquals(contextString.replaceAll(" ", ""), ct.getRequestMap().get("@context").toString().replaceAll(" ", ""));
 	}
@@ -223,10 +223,10 @@
 					"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
 						"{@type=korap:group, operation=operation:sequence, " +
 							"operands=[" +
-								"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
 									"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
 								"]}," +
-								"{@type=korap:group, operation=operation:class, class=1025, operands=[" +	
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +	
 									"{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
 								"]}" +
 							"], inOrder=true, " +
@@ -244,10 +244,10 @@
 				"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
 					"{@type=korap:group, operation=operation:sequence, " +
 						"operands=[" +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +	
+							"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +	
 								"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
 							"]}," +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +	
+							"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +	
 								"{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
 							"]}" +
 						"], inOrder=true, " +
@@ -267,10 +267,10 @@
 				"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
 					"{@type=korap:group, operation=operation:sequence, " +
 						"operands=[" +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +	
+							"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +	
 								"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
 							"]}," +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +	
+							"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +	
 								"{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
 							"]}" +
 						"], inOrder=true, " +
@@ -290,10 +290,10 @@
 						"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
 							"{@type=korap:group, operation=operation:sequence, " +
 								"operands=[" +
-									"{@type=korap:group, operation=operation:class, class=1025, operands=[" +	
+									"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +	
 										"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
 									"]}," +
-									"{@type=korap:group, operation=operation:class, class=1025, operands=[" +	
+									"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +	
 										"{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
 									"]}" +
 								"], inOrder=true, " +
@@ -303,21 +303,21 @@
 							"}" +
 						"]}";
 //					"{@type=korap:group, operation=operation:or, operands=[" +
-//						"{@type=korap:group, operation=operation:position, frames=[frame:], sharedClasses=[sharedClasses:intersects], operands=[" +
-//							"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+//						"{@type=korap:group, operation=operation:position, frames=[frames:], sharedClasses=[sharedClasses:intersects], operands=[" +
+//							"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +	
 //								"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
 //							"]}," +
-//							"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+//							"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +	
 //								"{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
 //							"]}" +
 //						"], frame=frame:overlaps}," +
 //						"{@type=korap:reference, operation=operation:focus, classRef=[1024], operands=[" +
 //							"{@type=korap:group, operation=operation:sequence, " +
 //								"operands=[" +
-//									"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+//									"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +	
 //										"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
 //									"]}," +
-//									"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+//									"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +	
 //										"{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
 //									"]}" +
 //								"], inOrder=true, " +
@@ -338,10 +338,10 @@
 					"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
 						"{@type=korap:group, operation=operation:sequence, " +
 							"operands=[" +
-								"{@type=korap:group, operation=operation:class, class=1025, operands=[" +	
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +	
 									"{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
 								"]}," +
-								"{@type=korap:group, operation=operation:class, class=1025, operands=[" +	
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +	
 									"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
 								"]}" +
 							"], inOrder=true, " +
@@ -351,21 +351,21 @@
 						"}" +
 					"]}";
 //				"{@type=korap:group, operation=operation:or, operands=[" +
-//					"{@type=korap:group, operation=operation:position, frames=[frame:], sharedClasses=[sharedClasses:intersects], operands=[" +
-//						"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+//					"{@type=korap:group, operation=operation:position, frames=[frames:], sharedClasses=[sharedClasses:intersects], operands=[" +
+//						"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +	
 //							"{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
 //						"]}," +
-//						"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+//						"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +	
 //							"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
 //						"]}" +
 //					"], frame=frame:overlaps}," +
 //					"{@type=korap:reference, operation=operation:focus, classRef=[1024], operands=[" +
 //						"{@type=korap:group, operation=operation:sequence, " +
 //							"operands=[" +
-//								"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+//								"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +	
 //									"{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
 //								"]}," +
-//								"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+//								"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +	
 //									"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
 //								"]}" +
 //							"], inOrder=true, " +
@@ -384,10 +384,10 @@
 					"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
 						"{@type=korap:group, operation=operation:sequence, " +
 							"operands=[" +
-								"{@type=korap:group, operation=operation:class, class=1025, operands=[" +	
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +	
 									"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
 								"]}," +
-								"{@type=korap:group, operation=operation:class, class=1025, operands=[" +	
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +	
 									"{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
 								"]}" +
 							"], inOrder=false, " +
@@ -397,21 +397,21 @@
 						"}" +
 					"]}";
 //				"{@type=korap:group, operation=operation:or, operands=[" +
-//					"{@type=korap:group, operation=operation:position, frames=[frame:], sharedClasses=[sharedClasses:intersects], operands=[" +
-//						"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+//					"{@type=korap:group, operation=operation:position, frames=[frames:], sharedClasses=[sharedClasses:intersects], operands=[" +
+//						"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +	
 //							"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
 //						"]}," +
-//						"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+//						"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +	
 //							"{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
 //						"]}" +
 //					"], frame=frame:overlaps}," +
 //					"{@type=korap:reference, operation=operation:focus, classRef=[1024], operands=[" +
 //						"{@type=korap:group, operation=operation:sequence, " +
 //							"operands=[" +
-//								"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+//								"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +	
 //									"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
 //								"]}," +
-//								"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+//								"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +	
 //									"{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
 //								"]}" +
 //							"], inOrder=false, " +
@@ -433,17 +433,17 @@
 					"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
 						"{@type=korap:group, operation=operation:sequence, " +
 							"operands=[" +
-								"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
 									"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
 								"]}," +
-								"{@type=korap:group, operation=operation:class, class=1025, operands=[" +	
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +	
 									"{@type=korap:reference, operation=operation:focus, classRef=[1026], operands=[" +
 										"{@type=korap:group, operation=operation:sequence, " +
 											"operands=[" +
-												"{@type=korap:group, operation=operation:class, class=1026, operands=[" +
+												"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
 													"{@type=korap:token, wrap={@type=korap:term, key=Sterne, layer=orth, match=match:eq}}" +
 												"]}," +
-												"{@type=korap:group, operation=operation:class, class=1026, operands=[" +	
+												"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +	
 													"{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
 												"]}" +
 											"], inOrder=true, " +
@@ -471,10 +471,10 @@
 //					"{@type=korap:token, wrap={@type=korap:term, key=Sterne, layer=orth, match=match:eq}}" ;
 		
 		String mondsterneClasses = 
-					"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+					"{@type=korap:group, operation=operation:class, class=1024 , classOut=1024, operands=[" +	
 						"{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
 					"]}," +
-					"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+					"{@type=korap:group, operation=operation:class, class=1024 , classOut=1024, operands=[" +	
 						"{@type=korap:token, wrap={@type=korap:term, key=Sterne, layer=orth, match=match:eq}}" +
 					"]}";
 //		String mondsterneSeq = 
@@ -492,11 +492,11 @@
 					"]}" +
 				"]}" ;
 //		String mondsterneOv =
-//					"{@type=korap:group, operation=operation:position, frames=[frame:], sharedClasses=[sharedClasses:intersects], operands=[" +
+//					"{@type=korap:group, operation=operation:position, frames=[frames:], sharedClasses=[sharedClasses:intersects], operands=[" +
 //						mondsterne +
 //					"]}";
 		String mondsterneClassesOv =
-				"{@type=korap:group, operation=operation:position, frames=[frame:], sharedClasses=[sharedClasses:intersects], operands=[" +
+				"{@type=korap:group, operation=operation:position, frames=[frames:], sharedClasses=[sharedClasses:intersects], operands=[" +
 					mondsterneClasses +
 				"], frame=frame:overlaps}";
 //		String mondsterneAll =
@@ -513,14 +513,14 @@
 				"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
 						"{@type=korap:group, operation=operation:sequence, " +
 							"operands=[" +
-								"{@type=korap:group, operation=operation:class, class=1025, operands=[" +	
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +	
 									"{@type=korap:reference, operation=operation:focus, classRef=[1026], operands=[" +
 										"{@type=korap:group, operation=operation:sequence, " +
 											"operands=[" +
-												"{@type=korap:group, operation=operation:class, class=1026, operands=[" +
+												"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
 													"{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
 												"]}," +
-												"{@type=korap:group, operation=operation:class, class=1026, operands=[" +	
+												"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +	
 													"{@type=korap:token, wrap={@type=korap:term, key=Sterne, layer=orth, match=match:eq}}" +
 												"]}" +
 											"], inOrder=true, " +
@@ -530,7 +530,7 @@
 										"}" +
 									"]}" +
 								"]}," +
-								"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
 									"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
 								"]}" +
 							"], inOrder=true, " +
@@ -541,21 +541,21 @@
 					"]}" +
 					""	;
 //			"{@type=korap:group, operation=operation:or, operands=[" +
-//				"{@type=korap:group, operation=operation:position, frames=[frame:], sharedClasses=[sharedClasses:intersects], operands=[" +
-//					"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+//				"{@type=korap:group, operation=operation:position, frames=[frames:], sharedClasses=[sharedClasses:intersects], operands=[" +
+//					"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +	
 //						mondsterneAllClasses  + 
 //					"]}," +
-//					"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+//					"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +	
 //						"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
 //					"]}" +
 //				"], frame=frame:overlaps}," +
 //				"{@type=korap:reference, operation=operation:focus, classRef=[1024], operands=[" +
 //					"{@type=korap:group, operation=operation:sequence, " +
 //						"operands=[" +
-//							"{@type=korap:group, operation=operation:class, class=1024, operands=[" +	
+//							"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +	
 //								mondsterneAllClasses  + 
 //							"]}," +
-//							"{@type=korap:group, operation=operation:class, class=1024, operands=[" +
+//							"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +
 //								"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
 //							"]}" +
 //						"], inOrder=true, " +
@@ -574,13 +574,17 @@
 	public void testOPIN() throws QueryException {
 		query="wegen #IN <s>";
 		String opin1 = 
-					"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
-						"{@type=korap:group, operation=operation:position, operands=[" +
-							"{@type=korap:span, key=s}," +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
-								"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
-							"]}" +
-						"], frames=[], sharedClasses=[sharedClasses:includes], frame=frame:contains}" +
+					"{@type=korap:reference, operation=operation:focus, classRef=[1026], operands=[" +
+						"{@type=korap:group, operation=operation:class, classRefCheck=classRefCheck:includes, classIn=[1025,1026], classOut=1027, operands=[" +
+							"{@type=korap:group, operation=operation:position, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
+									"{@type=korap:span, key=s}" +
+								"]}," +
+								"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
+									"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
+								"]}" +
+							"], frames=[], frame=frame:contains}" +
+						"]}" +
 					"]}";
 		ct = new CosmasTree(query);
 		map = ct.getRequestMap().get("query").toString();
@@ -588,14 +592,18 @@
 		
 		query="wegen #IN(L) <s>"; 
 		String opin2 = 
-					"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
-						"{@type=korap:group, operation=operation:position, operands=[" +
-							"{@type=korap:span, key=s}," +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
-								"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
-							"]}" +
-						"], frames=[frame:startswith], sharedClasses=[sharedClasses:includes], frame=frame:startswith}" +
-					"]}";
+				"{@type=korap:reference, operation=operation:focus, classRef=[1026], operands=[" +
+						"{@type=korap:group, operation=operation:class, classRefCheck=classRefCheck:includes, classIn=[1025,1026], classOut=1027, operands=[" +
+							"{@type=korap:group, operation=operation:position, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
+									"{@type=korap:span, key=s}" +
+								"]}," +
+								"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
+									"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
+								"]}" +
+						"], frames=[frames:startswith], frame=frame:startswith}" +
+					"]}" +
+				"]}";
 		ct = new CosmasTree(query);
 		map = ct.getRequestMap().get("query").toString();
 		assertEquals(opin2.replaceAll(" ", ""), map.replaceAll(" ", ""));
@@ -603,75 +611,92 @@
 		
 		query="wegen #IN(F) <s>";
 		String opin3= 
-					"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
-						"{@type=korap:group, operation=operation:position, operands=[" +
-							"{@type=korap:span, key=s}," +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
-								"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
-							"]}" +
-						"], frames=[frame:matches], sharedClasses=[sharedClasses:includes], frame=frame:matches}" +
+				"{@type=korap:reference, operation=operation:focus, classRef=[1026], operands=[" +
+						"{@type=korap:group, operation=operation:class, classRefCheck=classRefCheck:includes, classIn=[1025,1026], classOut=1027, operands=[" +
+							"{@type=korap:group, operation=operation:position, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
+									"{@type=korap:span, key=s}" +
+								"]}," +
+								"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
+									"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
+								"]}" +
+						"], frames=[frames:matches], frame=frame:matches}" +
 					"]}" +
-					"";
+				"]}";
 		ct = new CosmasTree(query);
 		map = ct.getRequestMap().get("query").toString();
 		assertEquals(opin3.replaceAll(" ", ""), map.replaceAll(" ", ""));
 		
 		query="wegen #IN(FI) <s>";
 		String opin4= 
-					"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
-						"{@type=korap:group, operation=operation:position, operands=[" +
-							"{@type=korap:span, key=s}," +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
-								"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
-							"]}" +
-						"], frames=[frame:matches], sharedClasses=[sharedClasses:unequals,sharedClasses:includes], frame=frame:matches-noident}" +
+				"{@type=korap:reference, operation=operation:focus, classRef=[1026], operands=[" +
+						"{@type=korap:group, operation=operation:class, classRefCheck=[classRefCheck:unequals,classRefCheck:includes], classIn=[1025,1026], classOut=1027, operands=[" +
+							"{@type=korap:group, operation=operation:position, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
+									"{@type=korap:span, key=s}" +
+								"]}," +
+								"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
+									"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
+								"]}" +
+						"], frames=[frames:matches], frame=frame:matches-noident}" +
 					"]}" +
-					"";
+				"]}";
 		ct = new CosmasTree(query);
 		map = ct.getRequestMap().get("query").toString();
 		assertEquals(opin4.replaceAll(" ", ""), map.replaceAll(" ", ""));
 		
 		query="wegen #IN(FE) <s>";
 		String opin5= 
-				"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
-						"{@type=korap:group, operation=operation:position, operands=[" +
-							"{@type=korap:span, key=s}," +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
-								"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
-							"]}" +
-						"], frames=[frame:matches], sharedClasses=[sharedClasses:equals], frame=frame:matches}" +
+				"{@type=korap:reference, operation=operation:focus, classRef=[1026], operands=[" +
+						"{@type=korap:group, operation=operation:class, classRefCheck=classRefCheck:equals, classIn=[1025,1026], classOut=1027, operands=[" +
+							"{@type=korap:group, operation=operation:position, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
+									"{@type=korap:span, key=s}" +
+								"]}," +
+								"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
+									"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
+								"]}" +
+						"], frames=[frames:matches], frame=frame:matches}" +
 					"]}" +
-					"";
+				"]}";
 		ct = new CosmasTree(query);
 		map = ct.getRequestMap().get("query").toString();
 		assertEquals(opin5.replaceAll(" ", ""), map.replaceAll(" ", ""));
 		
 		query="wegen #IN(%, L) <s>";
 		String opin6 = 
-					"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
-						"{@type=korap:group, operation=operation:position, operands=[" +
-							"{@type=korap:span, key=s}," +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
-								"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
-							"]}" +
-						"], frames=[frame:startswith], sharedClasses=[sharedClasses:includes], frame=frame:startswith, exclude=true}" +
-					"]}";
+				"{@type=korap:reference, operation=operation:focus, classRef=[1026], operands=[" +
+						"{@type=korap:group, operation=operation:class, classRefCheck=classRefCheck:includes, classIn=[1025,1026], classOut=1027, operands=[" +
+							"{@type=korap:group, operation=operation:position, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
+									"{@type=korap:span, key=s}" +
+								"]}," +
+								"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
+									"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
+								"]}" +
+						"], frames=[frames:startswith], frame=frame:startswith, exclude=true}" +
+					"]}" +
+				"]}";
 		ct = new CosmasTree(query);
 		map = ct.getRequestMap().get("query").toString();
 		assertEquals(opin6.replaceAll(" ", ""), map.replaceAll(" ", ""));
 		
 		query="wegen #IN('FE,ALL,%,MIN') <s>";
 		String opin7 = 
-					"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
+				"{@type=korap:reference, operation=operation:focus, reset=true, operands=[" +	
+					"{@type=korap:group, operation=operation:class, classRefCheck=classRefCheck:equals, classIn=[1025,1026], classOut=1027, operands=[" +
 						"{@type=korap:group, operation=operation:position, operands=[" +
-							"{@type=korap:reference, operation=operation:focus, reset=true, operands=[" +
+							"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
 								"{@type=korap:span, key=s}" +
 							"]}," +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+							"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
 								"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
 							"]}" +
-						"], frames=[frame:matches], sharedClasses=[sharedClasses:equals], frame=frame:matches, exclude=true, grouping=false}" +
-					"]}";
+				
+						"], frames=[frames:matches], frame=frame:matches, exclude=true, grouping=false}" +
+					"]}" +
+				"]}" +
+				"";
 		ct = new CosmasTree(query);
 		map = ct.getRequestMap().get("query").toString();
 		assertEquals(opin7.replaceAll(" ", ""), map.replaceAll(" ", ""));
@@ -681,13 +706,17 @@
 	public void testOPOV() throws QueryException {
 		query="wegen #OV <s>";
 		String opov1 = 
-					"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
-						"{@type=korap:group, operation=operation:position, operands=[" +
-							"{@type=korap:span, key=s}," +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
-								"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
-							"]}" +
-						"], frames=[], sharedClasses=[sharedClasses:intersects], frame=frame:overlaps}" +
+					"{@type=korap:reference, operation=operation:focus, classRef=[1026], operands=[" +
+						"{@type=korap:group, operation=operation:class, classRefCheck=classRefCheck:intersects, classIn=[1025,1026], classOut=1027, operands=[" +
+							"{@type=korap:group, operation=operation:position, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
+									"{@type=korap:span, key=s}" +
+								"]}," +
+								"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
+									"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
+								"]}" +
+							"], frames=[], frame=frame:overlaps}" +
+						"]}" +
 					"]}";
 		ct = new CosmasTree(query);
 		map = ct.getRequestMap().get("query").toString();
@@ -695,13 +724,17 @@
 		
 		query="wegen #OV(L) <s>";
 		String opov2 = 
-					"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
-						"{@type=korap:group, operation=operation:position, operands=[" +
-							"{@type=korap:span, key=s}," +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
-								"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
-							"]}" +
-						"], frames=[frame:startswith,frame:overlapsLeft], sharedClasses=[sharedClasses:intersects], frame=frame:overlapsLeft}" +
+				"{@type=korap:reference, operation=operation:focus, classRef=[1026], operands=[" +
+						"{@type=korap:group, operation=operation:class, classRefCheck=classRefCheck:intersects, classIn=[1025,1026], classOut=1027, operands=[" +
+							"{@type=korap:group, operation=operation:position, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
+									"{@type=korap:span, key=s}" +
+								"]}," +
+								"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
+									"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
+								"]}" +
+							"], frames=[frames:startswith,frames:overlapsLeft], frame=frame:overlapsLeft}" +
+						"]}" +
 					"]}";
 		ct = new CosmasTree(query);
 		map = ct.getRequestMap().get("query").toString();
@@ -709,13 +742,18 @@
 		
 		query="wegen #OV(F) <s>";
 		String opov3= 
-					"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
-						"{@type=korap:group, operation=operation:position, operands=[" +
-							"{@type=korap:span, key=s}," +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
-								"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
-							"]}" +
-						"], frames=[frame:matches], sharedClasses=[sharedClasses:intersects], frame=frame:matches}" +
+				
+					"{@type=korap:reference, operation=operation:focus, classRef=[1026], operands=[" +
+						"{@type=korap:group, operation=operation:class, classRefCheck=classRefCheck:intersects, classIn=[1025,1026], classOut=1027, operands=[" +
+							"{@type=korap:group, operation=operation:position, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
+									"{@type=korap:span, key=s}" +
+								"]}," +
+								"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
+									"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
+								"]}" +
+							"], frames=[frames:matches], frame=frame:matches}" +
+						"]}" +
 					"]}";
 		ct = new CosmasTree(query);
 		map = ct.getRequestMap().get("query").toString();
@@ -723,13 +761,17 @@
 		
 		query="wegen #OV(FI) <s>";
 		String opov4= 
-					"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
-						"{@type=korap:group, operation=operation:position, operands=[" +
-							"{@type=korap:span, key=s}," +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
-								"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
-							"]}" +
-						"], frames=[frame:matches], sharedClasses=[sharedClasses:unequals], frame=frame:matches-noident}" +
+					"{@type=korap:reference, operation=operation:focus, classRef=[1026], operands=[" +
+						"{@type=korap:group, operation=operation:class, classRefCheck=classRefCheck:unequals, classIn=[1025,1026], classOut=1027, operands=[" +
+							"{@type=korap:group, operation=operation:position, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
+									"{@type=korap:span, key=s}" +
+								"]}," +
+								"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
+									"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
+								"]}" +
+							"], frames=[frames:matches], frame=frame:matches-noident}" +
+						"]}" +
 					"]}";
 		ct = new CosmasTree(query);
 		map = ct.getRequestMap().get("query").toString();
@@ -737,15 +779,18 @@
 		
 		query="wegen #OV(FE) <s>";
 		String opov5= 
-							"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
-								"{@type=korap:group, operation=operation:position, operands=[" +
-									"{@type=korap:span, key=s}," +
-									"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
-										"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
-									"]}" +
-								"], frames=[frame:matches], sharedClasses=[sharedClasses:equals], frame=frame:matches}" +
-							"]}" +
-					"";
+				"{@type=korap:reference, operation=operation:focus, classRef=[1026], operands=[" +
+						"{@type=korap:group, operation=operation:class, classRefCheck=classRefCheck:equals, classIn=[1025,1026], classOut=1027, operands=[" +
+							"{@type=korap:group, operation=operation:position, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
+									"{@type=korap:span, key=s}" +
+								"]}," +
+								"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
+									"{@type=korap:token, wrap={@type=korap:term, key=wegen, layer=orth, match=match:eq}}" +
+								"]}" +
+							"], frames=[frames:matches], frame=frame:matches}" +
+						"]}" +
+					"]}";
 		ct = new CosmasTree(query);
 		map = ct.getRequestMap().get("query").toString();
 		assertEquals(opov5.replaceAll(" ", ""), map.replaceAll(" ", ""));
@@ -790,10 +835,10 @@
 		
 //		String dermannSeq = "{@type=korap:group, operation=operation:sequence," +
 //								"operands = [" +
-//									"{@type=korap:group, operation=operation:class, class=1024, operands=[" +
+//									"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +
 //										"{@type=korap:token, wrap={@type=korap:term, key=der, layer=orth, match=match:eq}}" +
 //									"]}," +
-//									"{@type=korap:group, operation=operation:class, class=1024, operands=[" +
+//									"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +
 //										"{@type=korap:token, wrap={@type=korap:term, key=Mann, layer=orth, match=match:eq}}" +
 //									"]}" +
 //									
@@ -802,7 +847,7 @@
 //								"]}";
 //		
 //		String begDermannSeq = 
-////							"{@type=korap:group, operation=operation:class, class=1024, operands=[" +
+////							"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +
 //								"{@type=korap:reference, operation=operation:focus, spanRef=[0,1], operands=[" +
 //									dermannSeq +
 //								"]}" +
@@ -814,20 +859,20 @@
 		query="#BEG(der /w3:5 Mann) /+w10 kommt"; // nesting #BEG() in a distance group
 		String beg2 = 
 //					"{@type=korap:group, operation=operation:or, operands=[" +
-//						"{@type=korap:group, operation=operation:position, frames=[frame:], sharedClasses=[sharedClasses:intersects], operands=[" +
-//							"{@type=korap:group, operation=operation:class, class=1024, operands=[" +
+//						"{@type=korap:group, operation=operation:position, frames=[frames:], sharedClasses=[sharedClasses:intersects], operands=[" +
+//							"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +
 //								beg1 +
 //							"]}" + "," +
-//							"{@type=korap:group, operation=operation:class, class=1024, operands=[" +
+//							"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +
 //								kommt +
 //							"]}" +
 //						"], frame=frame:overlaps}," +
 //						"{@type=korap:reference, operation=operation:focus, classRef=[1024], operands=[" +
 //							"{@type=korap:group, operation=operation:sequence, operands=[" +
-//								"{@type=korap:group, operation=operation:class, class=1024, operands=[" +
+//								"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +
 //									beg1 + 
 //								"]}," +
-//								"{@type=korap:group, operation=operation:class, class=1024, operands=[" +
+//								"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +
 //									kommt +
 //								"]}" +
 //							"], inOrder=true, distances=[" +
@@ -838,10 +883,10 @@
 //		
 						"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
 							"{@type=korap:group, operation=operation:sequence, operands=[" +
-								"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
 									beg1 + 
 								"]}," +
-								"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
 									kommt +
 								"]}" +
 							"], inOrder=true, distances=[" +
@@ -855,21 +900,21 @@
 		query="kommt /+w10 #BEG(der /w3:5 Mann)";
 		String beg3 = 
 //				"{@type=korap:group, operation=operation:or, operands=[" +
-//						"{@type=korap:group, operation=operation:position, frames=[frame:], sharedClasses=[sharedClasses:intersects], operands=[" +
-//							"{@type=korap:group, operation=operation:class, class=1024, operands=[" +
+//						"{@type=korap:group, operation=operation:position, frames=[frames:], sharedClasses=[sharedClasses:intersects], operands=[" +
+//							"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +
 //								kommt + 
 ////							"," +
 //							"]}," +
-//							"{@type=korap:group, operation=operation:class, class=1024, operands=[" +
+//							"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +
 //								beg1+
 //							"]}" +
 //						"], frame=frame:overlaps}," +
 //						"{@type=korap:reference, operation=operation:focus, classRef=[1024], operands=[" +
 //							"{@type=korap:group, operation=operation:sequence, operands=[" +
-//								"{@type=korap:group, operation=operation:class, class=1024, operands=[" +
+//								"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +
 //									kommt +
 //								"]}," +
-//								"{@type=korap:group, operation=operation:class, class=1024, operands=[" +
+//								"{@type=korap:group, operation=operation:class, class= , classOut=1024, operands=[" +
 //									beg1+
 //								"]}" +
 //							"], inOrder=true, distances=[" +
@@ -880,10 +925,10 @@
 				
 						"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
 							"{@type=korap:group, operation=operation:sequence, operands=[" +
-								"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
 									kommt +
 								"]}," +
-								"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
 									beg1+
 								"]}" +
 							"], inOrder=true, distances=[" +
@@ -920,7 +965,7 @@
 					"{@type=korap:group, operation=operation:position, frame=frame:frame:contains, operands=[" +
 						"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
 							"{@type=korap:group, operation=operation:position, frame=frame:frame:contains, operands=[" +
-								"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+								"{@type=korap:group, operation=operation:class, class= , classOut=1025, operands=[" +
 									"{@type=korap:span, key=s}" +
 								"]}," +
 								"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
@@ -937,7 +982,7 @@
 					"{@type=korap:group, operation=operation:position, frame=frame:frame:contains, operands=[" +
 						"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
 							"{@type=korap:group, operation=operation:position, frame=frame:frame:contains, operands=[" +
-								"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+								"{@type=korap:group, operation=operation:class, class= , classOut=1025, operands=[" +
 									"{@type=korap:span, key=s}" +
 								"]}," +
 								"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
@@ -991,7 +1036,7 @@
 						"{@type=korap:group, operation=operation:position, frame=frame:frame:contains, operands=[" +
 							"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
 								"{@type=korap:group, operation=operation:position, frame=frame:frame:contains, operands=[" +
-									"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+									"{@type=korap:group, operation=operation:class, class= , classOut=1025, operands=[" +
 										"{@type=korap:span, key=s}" +
 									"]}," +
 									"{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}" +
@@ -1033,7 +1078,7 @@
 		String elem2 = 
 			"{@type=korap:span, key=w, attr=" +
 				"{@type=korap:termGroup, relation=relation:and, operands=[" +
-					"{@type=korap:term, layer=pos, key=N, match=match:eq}" +
+					"{@type=korap:term, layer=p, key=N, match=match:eq}" +
 				"]}" +
 			"}";
 		ct = new CosmasTree(query);
@@ -1044,8 +1089,8 @@
 		String elem3 = 
 			"{@type=korap:span, key=w, attr=" +
 				"{@type=korap:termGroup, relation=relation:and, operands=[" +
-					"{@type=korap:term, layer=pos, key=N, match=match:ne}," +
-					"{@type=korap:term, layer=pos, key=V, match=match:ne}" +
+					"{@type=korap:term, layer=p, key=N, match=match:ne}," +
+					"{@type=korap:term, layer=p, key=V, match=match:ne}" +
 				"]}" +
 			"}";
 		ct = new CosmasTree(query);
@@ -1057,9 +1102,9 @@
 			"{@type=korap:span, key=w, attr=" +
 				"{@type=korap:termGroup, relation=relation:and, operands=[" +
 					"{@type=korap:termGroup, relation=relation:and, operands=[" +
-						"{@type=korap:term, layer=pos, key=N, match=match:ne}," +
-						"{@type=korap:term, layer=pos, key=A, match=match:ne}," +
-						"{@type=korap:term, layer=pos, key=V, match=match:ne}" +
+						"{@type=korap:term, layer=p, key=N, match=match:ne}," +
+						"{@type=korap:term, layer=p, key=A, match=match:ne}," +
+						"{@type=korap:term, layer=p, key=V, match=match:ne}" +
 					"]}," +
 					"{@type=korap:term, layer=Genre, key=Sport, match=match:eq}" +
 				"]}" +
@@ -1073,8 +1118,8 @@
 			"{@type=korap:span, key=w, attr=" +
 				"{@type=korap:termGroup, relation=relation:and, operands=[" +
 					"{@type=korap:termGroup, relation=relation:and, operands=[" +
-						"{@type=korap:term, layer=pos, key=N, match=match:ne}," +
-						"{@type=korap:term, layer=pos, key=V, match=match:ne}" +
+						"{@type=korap:term, layer=p, key=N, match=match:ne}," +
+						"{@type=korap:term, layer=p, key=V, match=match:ne}" +
 					"]}," +
 					"{@type=korap:termGroup, relation=relation:and, operands=[" +
 						"{@type=korap:term, layer=Genre, key=Sport, match=match:ne}," +
@@ -1103,6 +1148,28 @@
 		ct = new CosmasTree(query);
 		map = ct.getRequestMap().get("query").toString();
 		assertEquals(all1.replaceAll(" ", ""), map.replaceAll(" ", ""));
+		
+		query="#ALL(gehen /w1:10 (voran /w1:4 schnell))";
+		String all2 =
+				"{@type=korap:group, operation=operation:sequence, " +
+					"operands=[" +
+						"{@type=korap:token, wrap={@type=korap:term, key=gehen, layer=orth, match=match:eq}}," +
+							"{@type=korap:group, operation=operation:sequence, operands=[" +
+									"{@type=korap:token, wrap={@type=korap:term, key=voran, layer=orth, match=match:eq}}," +
+									"{@type=korap:token, wrap={@type=korap:term, key=schnell, layer=orth, match=match:eq}}" +
+								"], inOrder=false, " +
+								"distances=[" +
+									"{@type=korap:distance, key=w, boundary={@type=korap:boundary, min=1, max=4}, min=1, max=4}" +
+								"]" +
+							"}" +
+					"], inOrder=false, " +
+					"distances=[" +
+						"{@type=korap:distance, key=w, boundary={@type=korap:boundary, min=1, max=10}, min=1, max=10}" +
+					"]" +
+				"}";
+		ct = new CosmasTree(query);
+		map = ct.getRequestMap().get("query").toString();
+		assertEquals(all2.replaceAll(" ", ""), map.replaceAll(" ", ""));
 	}
 	
 	@Test
@@ -1112,10 +1179,10 @@
 //				"{@type=korap:reference, operation=operation:focus, classRef=[1025], classRefOp=classRefOp:inversion, operands=[" +
 //					"{@type=korap:group, operation=operation:sequence, " +
 //						"operands=[" +
-//							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+//							"{@type=korap:group, operation=operation:class, class= , classOut=1025, operands=[" +
 //								"{@type=korap:token, wrap={@type=korap:term, key=gehen, layer=orth, match=match:eq}}" +
 //							"]}," +
-//							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+//							"{@type=korap:group, operation=operation:class, class= , classOut=1025, operands=[" +
 //								"{@type=korap:token, wrap={@type=korap:term, key=voran, layer=orth, match=match:eq}}" +
 //							"]}" +
 //						"], inOrder=false, " +
@@ -1124,27 +1191,87 @@
 //						"]" +
 //					"}" +
 //				"]}";
-		
+//		
 		String nhit1 = 
-				"{@type=korap:reference, operation=operation:focus, classRef=1025, operands=[" +
-					"{@type=korap:group, operation=operation:sequence, " +
-						"operands=[" +
-							"{@type=korap:token, wrap={@type=korap:term, key=gehen, layer=orth, match=match:eq}}" +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
-								"{@type=korap:group, operation=operation:repetition, operands=[" +
-									"{@type=korap:token}" +
-								"], boundary={@type=korap:boundary, min=1, max=10}, min=1, max=10}}" +
-							"]}," +	
-							"{@type=korap:token, wrap={@type=korap:term, key=voran, layer=orth, match=match:eq}}" +
-						"], inOrder=false, " +
-						"distances=[" +
-							"{@type=korap:distance, key=w, boundary={@type=korap:boundary, min=1, max=10}, min=1, max=10}" +
-						"]" +
-					"}" +
+				"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
+					"{@type=korap:group, operation=operation:class, classRefOp=classRefOp:inversion, classIn=[1026,1027], classOut=1025, operands=[" +
+						"{@type=korap:group, operation=operation:sequence, " +
+							"operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1026 , classOut=1026, operands=[" +
+									"{@type=korap:token, wrap={@type=korap:term, key=gehen, layer=orth, match=match:eq}}" +
+								"]}," +	
+								"{@type=korap:group, operation=operation:class, class=1027 , classOut=1027, operands=[" +
+									"{@type=korap:token, wrap={@type=korap:term, key=voran, layer=orth, match=match:eq}}" +
+								"]}" +	
+							"], inOrder=false, " +
+							"distances=[" +
+								"{@type=korap:distance, key=w, boundary={@type=korap:boundary, min=1, max=10}, min=1, max=10}" +
+							"]" +
+						"}" +
+					"]}" +
 				"]}";
 		ct = new CosmasTree(query);
 		map = ct.getRequestMap().get("query").toString();
 		assertEquals(nhit1.replaceAll(" ", ""), map.replaceAll(" ", ""));
+//		
+//		query="#NHIT(gehen %w1:10 voran)";
+//		String nhit2 = 
+//				"{@type=korap:reference, operation=operation:focus, classRef=1025, operands=[" +
+//					"{@type=korap:group, operation=operation:sequence, " +
+//						"operands=[" +
+//							"{@type=korap:token, wrap={@type=korap:term, key=gehen, layer=orth, match=match:eq}}" +
+//							"{@type=korap:group, operation=operation:class, class= , classOut=1025, operands=[" +
+//								"{@type=korap:group, operation=operation:repetition, operands=[" +
+//									"{@type=korap:token}" +
+//								"], boundary={@type=korap:boundary, min=1, max=10}, min=1, max=10}}" +
+//							"]}," +	
+//							"{@type=korap:token, wrap={@type=korap:term, key=voran, layer=orth, match=match:eq}}" +
+//						"], inOrder=false, " +
+//						"distances=[" +
+//							"{@type=korap:distance, key=w, boundary={@type=korap:boundary, min=1, max=10}, min=1, max=10}" +
+//						"]" +
+//					"}" +
+//				"]}";
+//		ct = new CosmasTree(query);
+//		map = ct.getRequestMap().get("query").toString();
+//		assertEquals(nhit2.replaceAll(" ", ""), map.replaceAll(" ", ""));
+		
+		query="#NHIT(gehen /+w1:10 voran /w1:10 Beispiel)";
+		String nhit3 = 
+				"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
+					"{@type=korap:group, operation=operation:class, classRefOp=classRefOp:inversion, classIn=[1026,1027], classOut=1025, operands=[" +
+						"{@type=korap:group, operation=operation:sequence, " +
+							"operands=[" +
+								"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
+									"{@type=korap:token, wrap={@type=korap:term, key=gehen, layer=orth, match=match:eq}}" +
+								"]}," +
+								"{@type=korap:group, operation=operation:class, class=1027, classOut=1027, operands=[" +
+									"{@type=korap:reference, operation=operation:focus, classRef=[1028], operands=[" +
+										"{@type=korap:group, operation=operation:sequence, " +
+											"operands=[" +
+												"{@type=korap:group, operation=operation:class, class=1028, classOut=1028, operands=[" +
+													"{@type=korap:token, wrap={@type=korap:term, key=voran, layer=orth, match=match:eq}}" +
+												"]}," +
+												"{@type=korap:group, operation=operation:class, class=1028, classOut=1028, operands=[" +	
+													"{@type=korap:token, wrap={@type=korap:term, key=Beispiel, layer=orth, match=match:eq}}" +
+												"]}" +
+											"], inOrder=false, " +
+											"distances=[" +
+												"{@type=korap:distance, key=w, boundary={@type=korap:boundary, min=1, max=10}, min=1, max=10}" +
+											"]" +
+										"}" +
+									"]}" +
+								"]}" +
+							"], inOrder=true, " +
+							"distances=[" +
+								"{@type=korap:distance, key=w, boundary={@type=korap:boundary, min=1, max=10}, min=1, max=10}" +
+							"]" +
+						"}" +
+					"]}" +
+				"]}";
+		ct = new CosmasTree(query);
+		map = ct.getRequestMap().get("query").toString();
+		assertEquals(nhit3.replaceAll(" ", ""), map.replaceAll(" ", ""));
 	}
 	
 	@Test
@@ -1152,9 +1279,9 @@
 		query = "#BED(der , sa)";
 		String bed1 = 
 				"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands= [" +
-					"{@type=korap:group, operation=operation:position, frames=[frame:startswith], sharedClasses=[sharedClasses:includes], operands=[" +
+					"{@type=korap:group, operation=operation:position, frames=[frames:startswith], operands=[" +
 						"{@type=korap:span, key=s}," +
-						"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+						"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
 							"{@type=korap:token, wrap={@type=korap:term, key=der, layer=orth, match=match:eq}}" +
 						"]}" +
 					"], frame=frame:startswith}" +
@@ -1166,9 +1293,9 @@
 		query = "#BED(der Mann , +pe)";
 		String bed2 = 
 				"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands= [" +
-						"{@type=korap:group, operation=operation:position, frames=[frame:endswith], sharedClasses=[sharedClasses:includes], operands=[" +
+						"{@type=korap:group, operation=operation:position, frames=[frames:endswith], operands=[" +
 							"{@type=korap:span, key=p}," +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+							"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
 								"{@type=korap:group, operation=operation:sequence, operands=[" +
 									"{@type=korap:token, wrap={@type=korap:term, key=der, layer=orth, match=match:eq}}," +
 									"{@type=korap:token, wrap={@type=korap:term, key=Mann, layer=orth, match=match:eq}}" +
@@ -1183,19 +1310,19 @@
 		query = "#BED(der Mann , sa,-pa)";
 		String bed3 = 
 				"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
-					"{@type=korap:group, operation=operation:position, frames=[frame:matches], sharedClasses=[sharedClasses:equals], operands=[" +
-						"{@type=korap:group, operation=operation:position, frames=[frame:startswith], sharedClasses=[sharedClasses:includes], operands=[" +
+					"{@type=korap:group, operation=operation:position, frames=[frames:matches], operands=[" +
+						"{@type=korap:group, operation=operation:position, frames=[frames:startswith], operands=[" +
 							"{@type=korap:span, key=s}," +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+							"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
 								"{@type=korap:group, operation=operation:sequence, operands=[" +
 									"{@type=korap:token, wrap={@type=korap:term, key=der, layer=orth, match=match:eq}}," +
 									"{@type=korap:token, wrap={@type=korap:term, key=Mann, layer=orth, match=match:eq}}" +
 								"]}" +
 							"]}" +
 						"], frame=frame:startswith}," +
-						"{@type=korap:group, operation=operation:position, frames=[frame:startswith], sharedClasses=[sharedClasses:includes], operands=[" +
+						"{@type=korap:group, operation=operation:position, frames=[frames:startswith], operands=[" +
 							"{@type=korap:span, key=p}," +
-							"{@type=korap:group, operation=operation:class, class=1026, operands=[" +
+							"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
 								"{@type=korap:group, operation=operation:sequence, operands=[" +
 									"{@type=korap:token, wrap={@type=korap:term, key=der, layer=orth, match=match:eq}}," +
 									"{@type=korap:token, wrap={@type=korap:term, key=Mann, layer=orth, match=match:eq}}" +
@@ -1215,9 +1342,9 @@
 		query = "Der:sa";
 		String col1 = 
 				"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
-					"{@type=korap:group, operation=operation:position, frames=[frame:startswith], sharedClasses=[sharedClasses:includes], operands=[" +
+					"{@type=korap:group, operation=operation:position, frames=[frames:startswith], operands=[" +
 						"{@type=korap:span, key=s}," +
-						"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+						"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
 							"{@type=korap:token, wrap={@type=korap:term, key=Der, layer=orth, match=match:eq}}" +
 						"]}" +
 					"], frame=frame:startswith}" +
@@ -1229,22 +1356,22 @@
 		query = "Mann:sa,-pa,+te)";
 		String col2 = 
 				"{@type=korap:reference, operation=operation:focus, classRef=[1025], operands=[" +
-					"{@type=korap:group, operation=operation:position, frames=[frame:matches], sharedClasses=[sharedClasses:equals], operands=[" +
-						"{@type=korap:group, operation=operation:position, frames=[frame:startswith], sharedClasses=[sharedClasses:includes], operands=[" +
+					"{@type=korap:group, operation=operation:position, frames=[frames:matches], operands=[" +
+						"{@type=korap:group, operation=operation:position, frames=[frames:startswith], operands=[" +
 							"{@type=korap:span, key=s}," +
-							"{@type=korap:group, operation=operation:class, class=1025, operands=[" +
+							"{@type=korap:group, operation=operation:class, class=1025, classOut=1025, operands=[" +
 									"{@type=korap:token, wrap={@type=korap:term, key=Mann, layer=orth, match=match:eq}}" +
 							"]}" +
 						"], frame=frame:startswith}," +
-						"{@type=korap:group, operation=operation:position, frames=[frame:startswith], sharedClasses=[sharedClasses:includes], operands=[" +
+						"{@type=korap:group, operation=operation:position, frames=[frames:startswith], operands=[" +
 							"{@type=korap:span, key=p}," +
-							"{@type=korap:group, operation=operation:class, class=1026, operands=[" +
+							"{@type=korap:group, operation=operation:class, class=1026, classOut=1026, operands=[" +
 									"{@type=korap:token, wrap={@type=korap:term, key=Mann, layer=orth, match=match:eq}}" +
 							"]}" +
 						"], frame=frame:startswith, exclude=true}," +
-						"{@type=korap:group, operation=operation:position, frames=[frame:endswith], sharedClasses=[sharedClasses:includes], operands=[" +
+						"{@type=korap:group, operation=operation:position, frames=[frames:endswith], operands=[" +
 							"{@type=korap:span, key=t}," +
-							"{@type=korap:group, operation=operation:class, class=1027, operands=[" +
+							"{@type=korap:group, operation=operation:class, class=1027, classOut=1027, operands=[" +
 									"{@type=korap:token, wrap={@type=korap:term, key=Mann, layer=orth, match=match:eq}}" +
 							"]}" +
 						"], frame=frame:endswith}" +