multiple predications support in AQL
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/AqlTree.java b/src/main/java/de/ids_mannheim/korap/query/serialize/AqlTree.java
index 2512d90..a6bf990 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/AqlTree.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/AqlTree.java
@@ -104,7 +104,7 @@
 	private List<String> mirroredPositionFrames = Arrays.asList(new String[]{});
 	List<ParseTree> globalLingTermNodes = new ArrayList<ParseTree>();
 	private int totalRelationCount;
-	private LinkedList<String> multiplyReferencedNodes = new LinkedList<String>();
+	private LinkedHashMap<String, Integer> refClassMapping = new LinkedHashMap<String, Integer>();
 	private LinkedHashMap<String, Integer> nodeReferencesTotal = new LinkedHashMap<String, Integer>();
 	private LinkedHashMap<String, Integer> nodeReferencesProcessed = new LinkedHashMap<String, Integer>();
 	public static boolean verbose = false;
@@ -287,9 +287,9 @@
 				}
 				// Get operands list before possible re-assignment of 'group' (see following 'if')
 				ArrayList<Object> operands  = (ArrayList<Object>) group.get("operands");
-				// Wrap in focus object in case other relations are following
+				// Wrap in reference object in case other relations are following
 				if (i < node.getChildCount()-2) {
-					group = wrapInReference(group);
+					group = wrapInReference(group, classCounter);
 				}
 				// Retrieve operands.
 				String ref1 = null;
@@ -299,49 +299,39 @@
 				// Operand 1
 				if (!getNodeCat(operandTree1.getChild(0)).equals("variableExpr")) {
 					ref1 = operandTree1.getChild(0).toStringTree(parser).substring(1);
-					System.err.println("Operand 1: #"+ref1);
 					operand1 = variableReferences.get(ref1);
 					if (nodeReferencesTotal.get(ref1) > 1) {
 						if (nodeReferencesProcessed.get(ref1) == 0) {
+							refClassMapping.put(ref1, classCounter);
 							operand1 = wrapInClass(operand1);
-//							group = wrapInFocus(group);
-//							classCounter++;
 							nodeReferencesProcessed.put(ref1, nodeReferencesProcessed.get(ref1)+1);
 						} else if (nodeReferencesProcessed.get(ref1)>0 && nodeReferencesTotal.get(ref1)>1) {
 							try {
-								operand1 = wrapInReference(operandStack.pop());
-								classCounter++;
+								operand1 = wrapInReference(operandStack.pop(), refClassMapping.get(ref1));
 							} catch (NoSuchElementException e) {
-//								operand1 = null;
-								operand1 = makeReference(classCounter);
+								operand1 = makeReference(refClassMapping.get(ref1));
 							}
 						}
 					}
 				}
-				System.err.println("Operand 1 done");
 				// Operand 2
 				if (!getNodeCat(operandTree2.getChild(0)).equals("variableExpr")) {
 					ref2 = operandTree2.getChild(0).toStringTree(parser).substring(1);
-					System.err.println("Operand 2: #"+ref2);
 					operand2 = variableReferences.get(ref2);
 					if (nodeReferencesTotal.get(ref2) > 1) {
 						if (nodeReferencesProcessed.get(ref2)==0) {
+							refClassMapping.put(ref2, classCounter);
 							operand2 = wrapInClass(operand2);
-//							group = wrapInFocus(group);
-//							classCounter++;
 							nodeReferencesProcessed.put(ref2, nodeReferencesProcessed.get(ref2)+1);
 						} else if (nodeReferencesProcessed.get(ref2)>0 && nodeReferencesTotal.get(ref2)>1) {
 							try {
-								operand2 = wrapInReference(operandStack.pop());
-								classCounter++;
+								operand2 = wrapInReference(operandStack.pop(), refClassMapping.get(ref2));
 							} catch (NoSuchElementException e) {
-//								operand2 = null;
-								operand2 = makeReference(classCounter);
+								operand2 = makeReference(refClassMapping.get(ref2));
 							}
 						}
 					}
 				}
-				System.err.println("Operand 2 done");
 				// Inject operands.
 				// -> Case distinction:
 				if (node.getChildCount()==3) {
@@ -356,7 +346,6 @@
 						// for the first operator, include both operands
 						if (operand1 != null) operands.add(operand1);
 						if (operand2 != null) operands.add(wrapInClass(operand2));
-						classCounter++;
 						// Don't put this into the super object directly but store on operandStack 
 						// (because this group will have to be an operand of a subsequent operator)
 						operandStack.push(group);
@@ -364,7 +353,6 @@
 					} else if (i < node.getChildCount()-2){
 						// for all intermediate operators, include other previous groups and 2nd operand. Store this on the operandStack, too.
 						if (operand2 != null) operands.add(wrapInClass(operand2));
-						classCounter++;
 						operands.add(0, operandStack.pop());
 						operandStack.push(group);
 					} else if (i == node.getChildCount()-2) {
@@ -377,7 +365,6 @@
 					putIntoSuperObject(group);
 					if (!operandStack.isEmpty()) {
 						operands.add(0, operandStack.pop());
-//						operandStack.clear();
 					}
 					objectStack.push(group);
 					stackedObjects++;
@@ -385,7 +372,6 @@
 					operandStack.push(group);
 				}
 			}
-			System.err.println(operandStack);
 		}
 		
 		if (nodeCat.equals("variableExpr")) {
@@ -465,8 +451,8 @@
 		openNodeCats.pop();
 	}
 
-	private LinkedHashMap<String, Object> wrapInReference(LinkedHashMap<String, Object> group) {
-		LinkedHashMap<String, Object> refGroup = makeReference(classCounter);
+	private LinkedHashMap<String, Object> wrapInReference(LinkedHashMap<String, Object> group, Integer classId) {
+		LinkedHashMap<String, Object> refGroup = makeReference(classId);
 		ArrayList<Object> operands = new ArrayList<Object>();
 		operands.add(group);
 		refGroup.put("operands", operands);
@@ -477,6 +463,7 @@
 	private LinkedHashMap<String, Object> wrapInClass(LinkedHashMap<String, Object> group) {
 		LinkedHashMap<String, Object> classGroup = makeClass(classCounter);
 		((ArrayList<Object>) classGroup.get("operands")).add(group);
+		classCounter++;
 		return classGroup;
 	}