First attempt to support class numbers from 1 to 255 - 1 is the new default class number
diff --git a/src/main/java/de/ids_mannheim/korap/KorapQuery.java b/src/main/java/de/ids_mannheim/korap/KorapQuery.java
index 38e2038..f517c4a 100644
--- a/src/main/java/de/ids_mannheim/korap/KorapQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/KorapQuery.java
@@ -58,7 +58,7 @@
 	STARTSWITH   = SpanWithinQuery.STARTSWITH,
 	MATCH        = SpanWithinQuery.MATCH;
 
-    private static final int MAX_CLASS_NUM = 127;
+    private static final int MAX_CLASS_NUM = 255; // 127;
 
     /**
      * Constructs a new base object for query generation.
@@ -282,8 +282,14 @@
 		    JsonNode distances;
 		    if (firstDistance.get("@type").asText().equals("korap:group"))
 			distances = firstDistance.get("operands");
+
+		    // Support korap distances
 		    else if (firstDistance.get("@type").asText().equals("korap:distance"))
 			distances = json.get("distances");
+
+		    // Support cosmas distances
+		    else if (firstDistance.get("@type").asText().equals("cosmas:distance"))
+			distances = json.get("distances");
 		    else
 			throw new QueryException(612, "No valid distances defined");
 
@@ -292,11 +298,15 @@
 			if (constraint.has("key"))
 			    unit = constraint.get("key").asText();
 
+			/*
 			if (unit.equals("t"))
 			    throw new QueryException(
 				613,
 			        "Text based distances are not supported yet"
                             );
+			*/
+
+
 
 			int min = 0, max = 100;
 			if (constraint.has("boundary")) {
@@ -311,6 +321,20 @@
 				max = constraint.get("max").asInt(100);
 			};
 
+			// Add foundry and layer to the unit for new indices
+			if (constraint.has("foundry") &&
+			    constraint.has("layer") &&
+			    constraint.get("foundry").asText().length() > 0 &&
+			    constraint.get("layer").asText().length() > 0) {
+
+			    StringBuilder value = new StringBuilder();
+			    value.append(constraint.get("foundry").asText());
+			    value.append('/');
+			    value.append(constraint.get("layer").asText());
+			    value.append(':').append(unit);
+			    unit = value.toString();
+			};
+
 			// Sanitize boundary
 			if (max < min)
 			    max = min;
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanClassQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanClassQuery.java
index 16730ce..e493c9e 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanClassQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanClassQuery.java
@@ -24,22 +24,18 @@
 public class SpanClassQuery extends SpanQuery {
     public String field;
     protected byte number;
-    protected SpanQuery highlight;
+    protected SpanQuery operand;
 
-    public SpanClassQuery (SpanQuery highlight, byte number) {
-	this.field = highlight.getField();
-	this.highlight = highlight;
-	if (number <= 15) {
-	    this.number = number;
-	} else{
-	    this.number = (byte) 0;
-	};
+    public SpanClassQuery (SpanQuery operand, byte number) {
+	this.field = operand.getField();
+	this.operand = operand;
+	this.number = number;
     };
 
-    public SpanClassQuery (SpanQuery highlight) {
-	this.field = highlight.getField();
-	this.highlight = highlight;
-	this.number = (byte) 0;
+    public SpanClassQuery (SpanQuery operand) {
+	this.field = operand.getField();
+	this.operand = operand;
+	this.number = (byte) 1;
     };
 
     public byte number () {
@@ -51,14 +47,15 @@
 
     @Override
     public void extractTerms (Set<Term> terms) {
-	this.highlight.extractTerms(terms);
+	this.operand.extractTerms(terms);
     };
 
     @Override
     public String toString (String field) {
 	StringBuffer buffer = new StringBuffer("{");
-	buffer.append((int) this.number).append(": ");
-        buffer.append(this.highlight.toString()).append('}');
+	short classNr = (short) this.number;
+	buffer.append(classNr & 0xFF).append(": ");
+        buffer.append(this.operand.toString()).append('}');
 	buffer.append(ToStringUtils.boost(getBoost()));
 	return buffer.toString();
     };
@@ -68,7 +65,7 @@
 			   Bits acceptDocs,
 			   Map<Term,TermContext> termContexts) throws IOException {
 	return (Spans) new ClassSpans(
-	    this.highlight,
+	    this.operand,
 	    context,
 	    acceptDocs,
 	    termContexts,
@@ -79,12 +76,12 @@
     @Override
     public Query rewrite (IndexReader reader) throws IOException {
 	SpanClassQuery clone = null;
-	SpanQuery query = (SpanQuery) this.highlight.rewrite(reader);
+	SpanQuery query = (SpanQuery) this.operand.rewrite(reader);
 
-	if (query != this.highlight) {
+	if (query != this.operand) {
 	    if (clone == null)
 		clone = this.clone();
-	    clone.highlight = query;
+	    clone.operand = query;
 	};
 
 	if (clone != null)
@@ -96,7 +93,7 @@
     @Override
     public SpanClassQuery clone() {
 	SpanClassQuery spanClassQuery = new SpanClassQuery(
-	    (SpanQuery) this.highlight.clone(),
+	    (SpanQuery) this.operand.clone(),
 	    this.number
         );
 	spanClassQuery.setBoost(getBoost());
@@ -112,7 +109,7 @@
 	
 	final SpanClassQuery spanClassQuery = (SpanClassQuery) o;
 	
-	if (!highlight.equals(spanClassQuery.highlight)) return false;
+	if (!this.operand.equals(spanClassQuery.operand)) return false;
 
 	if (this.number != spanClassQuery.number) return false;
 
@@ -124,7 +121,7 @@
     @Override
     public int hashCode() {
 	int result = 1;
-	result = highlight.hashCode();
+	result = operand.hashCode();
 	result += (int) number;
 	result ^= (result << 15) | (result >>> 18);
 	result += Float.floatToRawIntBits(getBoost());
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanMatchModifyClassQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanMatchModifyClassQuery.java
index d507c07..b86b818 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanMatchModifyClassQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanMatchModifyClassQuery.java
@@ -24,21 +24,21 @@
 public class SpanMatchModifyClassQuery extends SpanClassQuery {
     private boolean divide = false;
 
-    public SpanMatchModifyClassQuery (SpanQuery highlight, byte number, boolean divide) {
-	super(highlight, number);
+    public SpanMatchModifyClassQuery (SpanQuery operand, byte number, boolean divide) {
+	super(operand, number);
 	this.divide = divide;
     };
 
-    public SpanMatchModifyClassQuery (SpanQuery highlight, boolean divide) {
-	this(highlight, (byte) 0, divide);
+    public SpanMatchModifyClassQuery (SpanQuery operand, boolean divide) {
+	this(operand, (byte) 1, divide);
     };
 
-    public SpanMatchModifyClassQuery (SpanQuery highlight, byte number) {
-	this(highlight, number, false);
+    public SpanMatchModifyClassQuery (SpanQuery operand, byte number) {
+	this(operand, number, false);
     };
 
-    public SpanMatchModifyClassQuery (SpanQuery highlight) {
-	this(highlight, (byte) 0, false);
+    public SpanMatchModifyClassQuery (SpanQuery operand) {
+	this(operand, (byte) 1, false);
     };
 
     @Override
@@ -50,8 +50,9 @@
 	else {
 	    buffer.append("shrink(");
 	};
-	buffer.append((int) this.number).append(": ");
-        buffer.append(this.highlight.toString());
+	short classNr = (short) this.number;
+	buffer.append(classNr & 0xFF).append(": ");
+        buffer.append(this.operand.toString());
 	buffer.append(')');
 
 	buffer.append(ToStringUtils.boost(getBoost()));
@@ -60,18 +61,18 @@
 
     @Override
     public Spans getSpans (final AtomicReaderContext context, Bits acceptDocs, Map<Term,TermContext> termContexts) throws IOException {
-	return (Spans) new MatchModifyClassSpans(this.highlight, context, acceptDocs, termContexts, number, divide);
+	return (Spans) new MatchModifyClassSpans(this.operand, context, acceptDocs, termContexts, number, divide);
     };
 
     @Override
     public Query rewrite (IndexReader reader) throws IOException {
 	SpanMatchModifyClassQuery clone = null;
-	SpanQuery query = (SpanQuery) this.highlight.rewrite(reader);
+	SpanQuery query = (SpanQuery) this.operand.rewrite(reader);
 
-	if (query != this.highlight) {
+	if (query != this.operand) {
 	    if (clone == null)
 		clone = this.clone();
-	    clone.highlight = query;
+	    clone.operand = query;
 	};
 
 	if (clone != null)
@@ -83,7 +84,7 @@
     @Override
     public SpanMatchModifyClassQuery clone() {
 	SpanMatchModifyClassQuery spanMatchQuery = new SpanMatchModifyClassQuery(
-	    (SpanQuery) this.highlight.clone(),
+	    (SpanQuery) this.operand.clone(),
 	    this.number,
 	    this.divide
         );
@@ -100,7 +101,7 @@
 	
 	final SpanMatchModifyClassQuery spanMatchModifyClassQuery = (SpanMatchModifyClassQuery) o;
 	
-	if (!highlight.equals(spanMatchModifyClassQuery.highlight)) return false;
+	if (!this.operand.equals(spanMatchModifyClassQuery.operand)) return false;
 	if (this.number != spanMatchModifyClassQuery.number) return false;
 	if (this.divide != spanMatchModifyClassQuery.divide) return false;
 	return getBoost() == spanMatchModifyClassQuery.getBoost();
@@ -111,7 +112,7 @@
     @Override
     public int hashCode() {
 	int result = 1;
-	result = highlight.hashCode();
+	result = operand.hashCode();
 	result += number + 33_333;
 	result += divide ? 1 : 0;
 	result ^= (result << 15) | (result >>> 18);
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/ClassSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/ClassSpans.java
index cf12852..4b087cd 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/ClassSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/ClassSpans.java
@@ -24,7 +24,7 @@
     private List<byte[]> highlightedPayload;
     private final Spans spans;
     private byte number;
-    private SpanQuery highlight;
+    private SpanQuery operand;
     private Boolean hasmorespans = false;
 
     private ByteBuffer bb = ByteBuffer.allocate(9);
@@ -33,14 +33,14 @@
     // This advices the java compiler to ignore all loggings
     public static final boolean DEBUG = false;
 
-    public ClassSpans (SpanQuery highlight,
+    public ClassSpans (SpanQuery operand,
 		       AtomicReaderContext context,
 		       Bits acceptDocs,
 		       Map<Term,TermContext> termContexts,
 		       byte number) throws IOException {
-	spans = highlight.getSpans(context, acceptDocs, termContexts);
+	spans = operand.getSpans(context, acceptDocs, termContexts);
 	this.number = number;
-	this.highlight = highlight;
+	this.operand = operand;
 	this.highlightedPayload = new ArrayList<byte[]>(6);
     };
 
@@ -131,7 +131,7 @@
 
     @Override
     public String toString() {
-	return getClass().getName() + "(" + this.highlight.toString() + ")@" +
+	return getClass().getName() + "(" + this.operand.toString() + ")@" +
 	    (doc() + ":" + start() + "-" + end());
     };
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/MatchModifyClassSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/MatchModifyClassSpans.java
index a45e1ee..d6a6318 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/MatchModifyClassSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/MatchModifyClassSpans.java
@@ -37,7 +37,7 @@
     private final Logger log = LoggerFactory.getLogger(MatchModifyClassSpans.class);
 
     // This advices the java compiler to ignore all loggings
-    public static final boolean DEBUG = false;
+    public static final boolean DEBUG = true;
 
     private int start = -1, end;
     private int tempStart = 0, tempEnd = 0;
@@ -53,7 +53,7 @@
 	this.number    = number;
 	this.divide    = divide;
 	this.wrapQuery = wrapQuery;
-	this.bb        = ByteBuffer.allocate(9);
+	this.bb        = ByteBuffer.allocate(20);
 	this.wrappedPayload = new ArrayList<byte[]>(6);
     };
 
@@ -114,7 +114,7 @@
 		    if (payload.length == 9 && bb.get() == this.number) {
 			bb.rewind();
 			tempStart = bb.getInt();
-			tempEnd = bb.getInt();
+			tempEnd   = bb.getInt();
 
 			if (DEBUG)
 			    log.trace("Found matching class {}-{}", tempStart, tempEnd);
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanMatchModifyQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanMatchModifyQueryWrapper.java
index 7faadf8..bfdcdbf 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanMatchModifyQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanMatchModifyQueryWrapper.java
@@ -31,7 +31,7 @@
 
     public SpanMatchModifyQueryWrapper (SpanQueryWrapper subquery) {
 	this.subquery = subquery;
-	this.number = (byte) 0;
+	this.number = (byte) 1;
     };
 
     public SpanQuery toQuery () throws QueryException {
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestClassIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestClassIndex.java
index 8d0a05b..7368843 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestClassIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestClassIndex.java
@@ -97,18 +97,18 @@
 	assertEquals("totalResults", 3, kr.totalResults());
 	assertEquals("StartPos (0)", 1, kr.match(0).startPos);
 	assertEquals("EndPos (0)", 2, kr.match(0).endPos);
-	assertEquals("snippetBrackets (0)", "a[{b}]cabcab ...", kr.match(0).snippetBrackets());
+	assertEquals("snippetBrackets (0)", "a[{1:b}]cabcab ...", kr.match(0).snippetBrackets());
 	assertEquals("snippetHTML (0)", "<span class=\"context-left\">a</span><span class=\"match\">" +
-			"<em class=\"class-0 level-0\">b</em></span><span class=\"context-right\">cabcab<span " +
+			"<em class=\"class-1 level-0\">b</em></span><span class=\"context-right\">cabcab<span " +
 			"class=\"more\"></span></span>", kr.match(0).snippetHTML());
 
 	assertEquals("StartPos (1)", 4, kr.match(1).startPos);
 	assertEquals("EndPos (1)", 5, kr.match(1).endPos);
-	assertEquals("snippetBrackets (1)", "abca[{b}]cabac", kr.match(1).snippetBrackets());
+	assertEquals("snippetBrackets (1)", "abca[{1:b}]cabac", kr.match(1).snippetBrackets());
 	
 	assertEquals("StartPos (2)", 7, kr.match(2).startPos);
 	assertEquals("EndPos (2)", 8, kr.match(2).endPos);
-	assertEquals("snippetBrackets (2)", "... bcabca[{b}]ac", kr.match(2).snippetBrackets());
+	assertEquals("snippetBrackets (2)", "... bcabca[{1:b}]ac", kr.match(2).snippetBrackets());
 
 
 	sq = new SpanNextQuery(
@@ -167,20 +167,20 @@
 	// System.err.println(kr.toJSON());
 
 	assertEquals("totalResults", 1, kr.totalResults());
-	assertEquals("SnippetBrackets (0)", "abcabc[a{2:b{a}}]c", kr.match(0).snippetBrackets());
-	assertEquals("SnippetHTML (0)", "<span class=\"context-left\">abcabc</span><span class=\"match\">a<em class=\"class-2 level-0\">b<em class=\"class-0 level-1\">a</em></em></span><span class=\"context-right\">c</span>", kr.match(0).snippetHTML());
+	assertEquals("SnippetBrackets (0)", "abcabc[a{2:b{1:a}}]c", kr.match(0).snippetBrackets());
+	assertEquals("SnippetHTML (0)", "<span class=\"context-left\">abcabc</span><span class=\"match\">a<em class=\"class-2 level-0\">b<em class=\"class-1 level-1\">a</em></em></span><span class=\"context-right\">c</span>", kr.match(0).snippetHTML());
 
 	// Offset tokens
 	kr = ki.search(sq, 0, (short) 10, true, (short) 2, true, (short) 2);
 	assertEquals("totalResults", 1, kr.totalResults());
-	assertEquals("SnippetBrackets (0)", "... bc[a{2:b{a}}]c", kr.match(0).snippetBrackets());
-	assertEquals("SnippetHTML (0)", "<span class=\"context-left\"><span class=\"more\"></span>bc</span><span class=\"match\">a<em class=\"class-2 level-0\">b<em class=\"class-0 level-1\">a</em></em></span><span class=\"context-right\">c</span>", kr.match(0).snippetHTML());
+	assertEquals("SnippetBrackets (0)", "... bc[a{2:b{1:a}}]c", kr.match(0).snippetBrackets());
+	assertEquals("SnippetHTML (0)", "<span class=\"context-left\"><span class=\"more\"></span>bc</span><span class=\"match\">a<em class=\"class-2 level-0\">b<em class=\"class-1 level-1\">a</em></em></span><span class=\"context-right\">c</span>", kr.match(0).snippetHTML());
 
 	// Offset Characters
 	kr = ki.search(sq, 0, (short) 10, false, (short) 2, false, (short) 2);
 	assertEquals("totalResults", 1, kr.totalResults());
-	assertEquals("SnippetBrackets (0)", "... bc[a{2:b{a}}]c", kr.match(0).snippetBrackets());
-	assertEquals("SnippetHTML (0)", "<span class=\"context-left\"><span class=\"more\"></span>bc</span><span class=\"match\">a<em class=\"class-2 level-0\">b<em class=\"class-0 level-1\">a</em></em></span><span class=\"context-right\">c</span>", kr.match(0).snippetHTML());
+	assertEquals("SnippetBrackets (0)", "... bc[a{2:b{1:a}}]c", kr.match(0).snippetBrackets());
+	assertEquals("SnippetHTML (0)", "<span class=\"context-left\"><span class=\"more\"></span>bc</span><span class=\"match\">a<em class=\"class-2 level-0\">b<em class=\"class-1 level-1\">a</em></em></span><span class=\"context-right\">c</span>", kr.match(0).snippetHTML());
 
 
 	// System.err.println(kr.toJSON());
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestMatchIdentifier.java b/src/test/java/de/ids_mannheim/korap/index/TestMatchIdentifier.java
index 347f4ee..3acc821 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestMatchIdentifier.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestMatchIdentifier.java
@@ -101,8 +101,8 @@
 
 	KorapMatch km = kr.match(0);
 
-	assertEquals("SnippetBrackets (0)", "... bcabca[{2:b{a}}]c", km.snippetBrackets());
-	assertEquals("ID (0)", "match-c1!d1-p7-9(2)7-8(0)8-8", km.getID());
+	assertEquals("SnippetBrackets (0)", "... bcabca[{2:b{1:a}}]c", km.snippetBrackets());
+	assertEquals("ID (0)", "match-c1!d1-p7-9(2)7-8(1)8-8", km.getID());
     };
 
     @Test
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestMatchIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestMatchIndex.java
index 529b74a..fb8850d 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestMatchIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestMatchIndex.java
@@ -71,9 +71,9 @@
 	assertEquals("totalResults", 1, kr.totalResults());
 	assertEquals("StartPos (0)", 7, kr.match(0).startPos);
 	assertEquals("EndPos (0)", 9, kr.match(0).endPos);
-	assertEquals("SnippetBrackets (0)", "... bcabca[b{a}]c", kr.match(0).snippetBrackets());
+	assertEquals("SnippetBrackets (0)", "... bcabca[b{1:a}]c", kr.match(0).snippetBrackets());
 
-	assertEquals("Test no 'more' context", "<span class=\"context-left\"><span class=\"more\"></span>bcabca</span><span class=\"match\">b<em class=\"class-0 level-0\">a</em></span><span class=\"context-right\">c</span>", kr.match(0).snippetHTML());
+	assertEquals("Test no 'more' context", "<span class=\"context-left\"><span class=\"more\"></span>bcabca</span><span class=\"match\">b<em class=\"class-1 level-0\">a</em></span><span class=\"context-right\">c</span>", kr.match(0).snippetHTML());
 	sq = new SpanMatchModifyClassQuery(
   	     new SpanNextQuery(
                 new SpanTermQuery(new Term("base", "s:b")),
@@ -133,23 +133,23 @@
 	// System.err.println(kr.toJSON());
 
 	assertEquals("totalResults", 1, kr.totalResults());
-	assertEquals("SnippetBrackets (0)", "... bcabca[b{a}]c", kr.match(0).snippetBrackets());
+	assertEquals("SnippetBrackets (0)", "... bcabca[b{1:a}]c", kr.match(0).snippetBrackets());
 
-	assertEquals("SnippetHTML (0) 1", "<span class=\"context-left\"><span class=\"more\"></span>bcabca</span><span class=\"match\">b<em class=\"class-0 level-0\">a</em></span><span class=\"context-right\">c</span>", kr.match(0).snippetHTML());
+	assertEquals("SnippetHTML (0) 1", "<span class=\"context-left\"><span class=\"more\"></span>bcabca</span><span class=\"match\">b<em class=\"class-1 level-0\">a</em></span><span class=\"context-right\">c</span>", kr.match(0).snippetHTML());
 
 	// Offset tokens
 	kr = ki.search(sq, 0, (short) 10, true, (short) 2, true, (short) 2);
 	assertEquals("totalResults", 1, kr.totalResults());
-	assertEquals("SnippetBrackets (0)", "... ca[b{a}]c", kr.match(0).snippetBrackets());
+	assertEquals("SnippetBrackets (0)", "... ca[b{1:a}]c", kr.match(0).snippetBrackets());
 
 
 
 	// Offset Characters
 	kr = ki.search(sq, 0, (short) 10, false, (short) 1, false, (short) 0);
 	assertEquals("totalResults", 1, kr.totalResults());
-	assertEquals("SnippetBrackets (0)", "... a[b{a}] ...", kr.match(0).snippetBrackets());
+	assertEquals("SnippetBrackets (0)", "... a[b{1:a}] ...", kr.match(0).snippetBrackets());
 
-	assertEquals("SnippetHTML (0) 2", "<span class=\"context-left\"><span class=\"more\"></span>a</span><span class=\"match\">b<em class=\"class-0 level-0\">a</em></span><span class=\"context-right\"><span class=\"more\"></span></span>", kr.match(0).snippetHTML());
+	assertEquals("SnippetHTML (0) 2", "<span class=\"context-left\"><span class=\"more\"></span>a</span><span class=\"match\">b<em class=\"class-1 level-0\">a</em></span><span class=\"context-right\"><span class=\"more\"></span></span>", kr.match(0).snippetHTML());
 
 	sq = new SpanMatchModifyClassQuery(
             new SpanClassQuery(
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestKorapQuery.java b/src/test/java/de/ids_mannheim/korap/query/TestKorapQuery.java
index 2eaa1cd..c5fd542 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestKorapQuery.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestKorapQuery.java
@@ -113,21 +113,21 @@
     public void KorapClassQuery2 () throws QueryException {
 	KorapQuery kq = new KorapQuery("field");
 	SpanQuery sq = kq._(kq.seg("base:test")).toQuery();
-	assertEquals("{0: field:base:test}", sq.toString());
+	assertEquals("{1: field:base:test}", sq.toString());
     };
 
     @Test
     public void KorapClassQuery3 () throws QueryException {
 	KorapQuery kq = new KorapQuery("field");
 	SpanQuery sq = kq.seq(kq.seg("tree"), kq.contains(kq.tag("s"), kq._(kq.tag("np"))), kq.re("hey.*")).toQuery();
-	assertEquals("spanNext(spanNext(field:tree, spanContain(<field:s />, {0: <field:np />})), SpanMultiTermQueryWrapper(field:/hey.*/))", sq.toString());
+	assertEquals("spanNext(spanNext(field:tree, spanContain(<field:s />, {1: <field:np />})), SpanMultiTermQueryWrapper(field:/hey.*/))", sq.toString());
     };
 
     @Test
     public void KorapShrinkQuery () throws QueryException {
 	KorapQuery kq = new KorapQuery("field");
 	SpanQuery sq = kq.shrink(kq.tag("np")).toQuery();
-	assertEquals("shrink(0: <field:np />)", sq.toString());
+	assertEquals("shrink(1: <field:np />)", sq.toString());
     };
 
     @Test
@@ -148,7 +148,7 @@
     public void KorapShrinkQuery3 () throws QueryException {
 	KorapQuery kq = new KorapQuery("field");
 	SpanQuery sq = kq.shrink(1, kq._(1, kq.seq(kq.tag("np"), kq._(kq.seg("test").without("no"))))).toQuery();
-	assertEquals("shrink(1: {1: spanNext(<field:np />, {0: spanNot(field:test, field:no, 0, 0)})})", sq.toString());
+	assertEquals("shrink(1: {1: spanNext(<field:np />, {1: spanNot(field:test, field:no, 0, 0)})})", sq.toString());
     };
 
     @Test
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestKorapQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestKorapQueryJSON.java
index c0ad9d9..41cac0f 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestKorapQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestKorapQueryJSON.java
@@ -52,7 +52,7 @@
 	SpanQueryWrapper sqwi = jsonQuery(getClass().getResource("/queries/bsp3.jsonld").getFile());
 
 	// shrink({[base=Mann]})
-	assertEquals(sqwi.toQuery().toString(), "shrink(0: {0: tokens:mate/l:Mann})");
+	assertEquals(sqwi.toQuery().toString(), "shrink(1: {1: tokens:mate/l:Mann})");
     };
 
     @Test
@@ -60,7 +60,7 @@
 	SpanQueryWrapper sqwi = jsonQuery(getClass().getResource("/queries/bsp4.jsonld").getFile());
 
 	// shrink({[base=foo]}[orth=bar])
-	assertEquals(sqwi.toQuery().toString(), "shrink(0: spanNext({0: tokens:mate/l:foo}, tokens:s:bar))");
+	assertEquals(sqwi.toQuery().toString(), "shrink(1: spanNext({1: tokens:mate/l:foo}, tokens:s:bar))");
     };
 
     @Test
@@ -194,7 +194,7 @@
 	SpanQueryWrapper sqwi = jsonQuery(getClass().getResource("/queries/bsp-class.jsonld").getFile());
 
 	// within(<np>,[base=Mann])
-	assertEquals(sqwi.toQuery().toString(), "{0: spanNext(tokens:tt/p:ADJA, tokens:mate/p:NN)}");
+	assertEquals(sqwi.toQuery().toString(), "{1: spanNext(tokens:tt/p:ADJA, tokens:mate/p:NN)}");
     };
     
     @Test
@@ -277,6 +277,16 @@
 	assertEquals(sqwi.toQuery().toString(), "spanDistance(tokens:s:Tal, tokens:s:Wald, [(w[2:100], ordered, notExcluded)])");
     };
 
+    @Test
+    public void queryJSONcosmasBoundaryBug () throws QueryException {
+	SpanQueryWrapper sqwi = jsonQuery(getClass().getResource("/queries/bugs/cosmas_boundary.jsonld").getFile());
+
+	// Namen /s1 Leben
+	assertEquals(sqwi.toQuery().toString(), "shrink(129: spanElementDistance({129: tokens:s:Namen}, {129: tokens:s:Leben}, [(base/s:s[0:1], notOrdered, notExcluded)]))");
+    };
+
+
+
     public static String getString (String path) {
 	StringBuilder contentBuilder = new StringBuilder();
 	try {
diff --git a/src/test/java/de/ids_mannheim/korap/search/TestKorapSearch.java b/src/test/java/de/ids_mannheim/korap/search/TestKorapSearch.java
index 4fd0c21..8180a23 100644
--- a/src/test/java/de/ids_mannheim/korap/search/TestKorapSearch.java
+++ b/src/test/java/de/ids_mannheim/korap/search/TestKorapSearch.java
@@ -3,6 +3,8 @@
 import java.util.*;
 import java.io.*;
 
+import static de.ids_mannheim.korap.TestSimple.*;
+
 import de.ids_mannheim.korap.KorapSearch;
 import de.ids_mannheim.korap.KorapCollection;
 import de.ids_mannheim.korap.KorapQuery;
@@ -604,6 +606,42 @@
     };
 
     @Test
+    public void searchJSONcosmasBoundaryBug () throws IOException {
+	// Construct index
+	KorapIndex ki = new KorapIndex();
+	// Indexing test files
+	FieldDocument fd = ki.addDocFile(
+            1,getClass().getResource("/bzk/D59-00089.json.gz").getFile(), true
+	);
+	ki.commit();
+
+	String json = getString(
+	    getClass().getResource("/queries/bugs/cosmas_boundary.jsonld").getFile()
+        );
+
+	KorapQuery kq = new KorapQuery("tokens");
+
+	// Check with 129
+	KorapSearch ks = new KorapSearch(
+	    kq.shrink(1,kq.contains(kq.tag("base/s:s"), kq._(1, kq.seg("s:Leben"))))
+	);
+
+	KorapResult kr = ks.run(ki);
+	System.err.println(kr.getMatch(0).getSnippetBrackets());
+
+	/*
+
+	ks = new KorapSearch(json);
+
+	kr = ks.run(ki);
+	System.err.println(kr.toJSON());
+	assertEquals(276, kr.getTotalResults());
+	assertEquals(0, kr.getStartIndex());
+	assertEquals(10, kr.getItemsPerPage());
+	*/
+    };
+
+    @Test
     public void searchJSONCollection () throws IOException {
 
 	// Construct index
@@ -622,7 +660,9 @@
 	};
 	ki.commit();
 
-	String json = getString(getClass().getResource("/queries/metaquery8-nocollection.jsonld").getFile());
+	String json = getString(
+            getClass().getResource("/queries/metaquery8-nocollection.jsonld").getFile()
+        );
 	
 	KorapSearch ks = new KorapSearch(json);
 	KorapResult kr = ks.run(ki);
@@ -910,8 +950,6 @@
 	assertEquals(11, map.size());
     };
 
-
-
     public static String getString (String path) {
 	StringBuilder contentBuilder = new StringBuilder();
 	try {
diff --git a/src/test/resources/queries/bsp3.jsonld b/src/test/resources/queries/bsp3.jsonld
index 1a40038..0e76838 100644
--- a/src/test/resources/queries/bsp3.jsonld
+++ b/src/test/resources/queries/bsp3.jsonld
@@ -103,12 +103,12 @@
   "query": {
     "@type": "korap:group", 
     "operation": "operation:submatch", 
-    "classRef": [0],
+    "classRef": [1],
     "operands": [
       {
         "@type": "korap:group", 
         "operation" : "operation:class",
-        "class": 0, 
+        "class": 1, 
         "operands": [
           {
             "@type": "korap:token", 
diff --git a/src/test/resources/queries/bsp4.jsonld b/src/test/resources/queries/bsp4.jsonld
index c5cc7db..3a0c3c4 100644
--- a/src/test/resources/queries/bsp4.jsonld
+++ b/src/test/resources/queries/bsp4.jsonld
@@ -103,7 +103,7 @@
   "query": {
     "@type": "korap:group", 
     "operation": "operation:submatch", 
-    "classRef": [0],
+    "classRef": [1],
     "operands": [
       {
         "@type": "korap:group", 
@@ -112,7 +112,7 @@
           {
             "@type": "korap:group", 
 	    "operation":"operation:class",
-            "class": 0, 
+            "class": 1, 
             "operands": [
               {
                 "@type": "korap:token", 
diff --git a/src/test/resources/queries/bugs/cosmas_boundary.jsonld b/src/test/resources/queries/bugs/cosmas_boundary.jsonld
new file mode 100644
index 0000000..fb0f5ff
--- /dev/null
+++ b/src/test/resources/queries/bugs/cosmas_boundary.jsonld
@@ -0,0 +1,69 @@
+{
+  "@context": "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld",
+  "query":{
+    "@type" : "korap:reference",
+    "classRef" : [
+      129
+    ],
+    "operands" : [
+      {
+        "@type" : "korap:group",
+        "distances" : [
+          {
+            "@type" : "cosmas:distance",
+            "boundary" : {
+              "@type" : "korap:boundary",
+              "max" : 1,
+              "min" : 0
+            },
+	    "foundry" : "base",
+	    "layer" : "s",
+            "key" : "s",
+
+            "max" : 1,
+            "min" : 0
+          }
+        ],
+        "inOrder" : false,
+        "operands" : [
+          {
+            "@type" : "korap:group",
+            "class" : 129,
+            "classOut" : 129,
+            "operands" : [
+              {
+                "@type" : "korap:token",
+                "wrap" : {
+                  "@type" : "korap:term",
+                  "key" : "Namen",
+                  "layer" : "orth",
+                  "match" : "match:eq"
+                }
+              }
+            ],
+            "operation" : "operation:class"
+          },
+          {
+            "@type" : "korap:group",
+            "class" : 129,
+            "classOut" : 129,
+            "operands" : [
+              {
+                "@type" : "korap:token",
+                "wrap" : {
+                  "@type" : "korap:term",
+                  "key" : "Leben",
+                  "layer" : "orth",
+                  "match" : "match:eq"
+                }
+              }
+            ],
+            "operation" : "operation:class"
+          }
+        ],
+        "operation" : "operation:sequence"
+      }
+    ],
+    "operation" : "operation:focus"
+  }
+}