grouping behaviour (MIN/MAX options in #IN and #OV)
diff --git a/src/main/java/de/ids_mannheim/korap/query/serialize/Cosmas2QueryProcessor.java b/src/main/java/de/ids_mannheim/korap/query/serialize/Cosmas2QueryProcessor.java
index 09c68c3..b3e1d45 100644
--- a/src/main/java/de/ids_mannheim/korap/query/serialize/Cosmas2QueryProcessor.java
+++ b/src/main/java/de/ids_mannheim/korap/query/serialize/Cosmas2QueryProcessor.java
@@ -477,20 +477,18 @@
         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 and decide where to put
+        // Step II: wrap in focus and decide where to put
         ArrayList<String> check = 
                 (ArrayList<String>) positionOptions.get("classRefCheck");
         Integer[] classIn = 
                 new Integer[] { 128 + classCounter - 2, 128 + classCounter - 1 };
         LinkedHashMap<String, Object> classRefCheck = 
-                KoralObjectGenerator.makeClassRefCheck(check, classIn, 128 + classCounter);
+                KoralObjectGenerator.makeClassRefCheck(check, classIn,
+                        128 + classCounter);
         ((ArrayList<Object>) classRefCheck.get("operands")).add(posgroup);
         LinkedHashMap<String, Object> focusGroup = null;
         if ((boolean) positionOptions.get("matchall") == true) {
@@ -501,6 +499,17 @@
             focusGroup = KoralObjectGenerator.wrapInReference(classRefCheck,
                     128 + classCounter - 1);
         }
+        System.err.println(positionOptions);
+        // wrap in 'merge' operation if grouping option is set
+        if (positionOptions.containsKey("grouping")) {
+            if (positionOptions.get("grouping").equals(true)) {
+                LinkedHashMap<String, Object> mergeOperation = 
+                        KoralObjectGenerator.makeGroup("merge");
+                ArrayList<Object> mergeOperands = (ArrayList<Object>) mergeOperation.get("operands");
+                mergeOperands.add(focusGroup);
+                focusGroup = mergeOperation;
+            }
+        }
         putIntoSuperObject(focusGroup, 1);
     }
 
@@ -1128,14 +1137,17 @@
         }
 
         if (negatePosition) {
-            posOptions.put("exclude", "true");
+            posOptions.put("exclude", true);
         }
 
+        boolean grouping = false;
         if (groupnode != null) {
-            String grouping = groupnode.getChild(0).toStringTree()
-                    .equals("max") ? "true" : "false";
-            posOptions.put("grouping", grouping);
-        }
+            if (groupnode.getChild(0).toStringTree().equalsIgnoreCase("max")) {
+                grouping = true;
+            }
+        } 
+        posOptions.put("grouping", grouping);
+        
         return posOptions;
     }
 
@@ -1220,12 +1232,15 @@
         if (negatePosition) {
             posOptions.put("exclude", "true");
         }
-
+        
+        boolean grouping = false;
         if (groupnode != null) {
-            String grouping = groupnode.getChild(0).toStringTree()
-                    .equals("max") ? "true" : "false";
-            posOptions.put("grouping", grouping);
-        }
+            if (groupnode.getChild(0).toStringTree().equalsIgnoreCase("max")) {
+                grouping = true;
+            }
+        } 
+        posOptions.put("grouping", grouping);
+        
         return posOptions;
     }
 
diff --git a/src/test/java/Cosmas2QueryProcessorTest.java b/src/test/java/Cosmas2QueryProcessorTest.java
index a65ec95..e9c1815 100644
--- a/src/test/java/Cosmas2QueryProcessorTest.java
+++ b/src/test/java/Cosmas2QueryProcessorTest.java
@@ -621,6 +621,15 @@
 		assertEquals("classRefCheck:equals",		res.at("/query/operands/0/classRefCheck/0").asText());
 		assertEquals("frames:matches",				res.at("/query/operands/0/operands/0/frames/0").asText());
 		assertEquals(true,							res.at("/query/operands/0/operands/0/exclude").asBoolean());
+		
+		query = "wegen #IN(FE,ALL,%,MAX) <s>";
+        qs.setQuery(query, "cosmas2");
+        res = mapper.readTree(qs.toJSON());
+        assertEquals("operation:merge",             res.at("/query/operation").asText());
+        assertEquals(true,                          res.at("/query/operands/0/reset").asBoolean());
+        assertEquals("classRefCheck:equals",        res.at("/query/operands/0/operands/0/classRefCheck/0").asText());
+        assertEquals("frames:matches",              res.at("/query/operands/0/operands/0/operands/0/frames/0").asText());
+        assertEquals(true,                          res.at("/query/operands/0/operands/0/operands/0/exclude").asBoolean());
 	}
 
 	@Test