Minor modifications to the - still broken - test

Change-Id: I6cf1c785e21fe8b037e7a779d6fd79125c89a537
diff --git a/Changes b/Changes
index 965f5d6..250b625 100644
--- a/Changes
+++ b/Changes
@@ -1,3 +1,8 @@
+0.55.4 2016-02-25
+        - [bugfix] Wrap <base/s=t> boundaries around right extended queries
+	  (diewald)
+	- [bugfix] Lazy loading bug in WithinSpans (diewald)
+
 0.55.3 2016-02-22
 	- [bugfix] Use base foundry for default sentences and paragraphs
 	  (diewald, margaretha)
diff --git a/pom.xml b/pom.xml
index 26a55eb..47118ed 100644
--- a/pom.xml
+++ b/pom.xml
@@ -25,7 +25,7 @@
 
   <groupId>de.ids_mannheim.korap</groupId>
   <artifactId>Krill</artifactId>
-  <version>0.55.3</version>
+  <version>0.55.4</version>
   <packaging>jar</packaging>
 
   <name>Krill</name>
diff --git a/src/main/java/de/ids_mannheim/korap/Krill.java b/src/main/java/de/ids_mannheim/korap/Krill.java
index 8800604..ad72f7a 100644
--- a/src/main/java/de/ids_mannheim/korap/Krill.java
+++ b/src/main/java/de/ids_mannheim/korap/Krill.java
@@ -181,7 +181,7 @@
                     // Serialize a Lucene SpanQuery based on the SpanQueryWrapper
                     this.spanQuery = qw.toQuery();
 
-                    // TODO: Make these information query rewrites in toFinalQuery()
+                    // TODO: Make these information query rewrites
 
                     // Throw a warning in case the root object is optional
                     if (qw.isOptional())
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/WithinSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/WithinSpans.java
index 2afcc42..539c331 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/WithinSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/WithinSpans.java
@@ -24,6 +24,8 @@
  *
  * TODO: Support exclusivity
  * TODO: Use the term "queue" and implement it similar to SpanOrQuery
+ * TODO: Implement a incrStartPos() method to forward an embedded span
+ *       until the start position is higher than the current start position.
  */
 
 /**
@@ -282,13 +284,14 @@
                     this.more = true;
                     this.inSameDoc = true;
                     this.tryMatch = true;
-
+                    
                     this.nextSpanB();
                     continue;
                 }
 
                 // Fetch from second store?
                 else {
+
                     /** TODO: Change this to a single embedded object! */
                     this.embeddedStart = current.start;
                     this.embeddedEnd = current.end;
@@ -658,6 +661,8 @@
         this.wrapEnd = -1;
 
         // Shortcut to prevent lazyloading of .end()
+        //   [---
+        // [---
         if (this.wrapStart > this.embeddedStart) {
             // Can't match for in, rin, ew, sw, and m
             // and will always lead to next_b
@@ -669,6 +674,8 @@
             };
         }
 
+        // [---
+        //   [---
         else if (this.wrapStart < this.embeddedStart) {
             // Can't match for sw and m and will always
             // lead to next_a
@@ -973,6 +980,7 @@
 
             // Load wrapEnd
             this.wrapEnd = this.wrapSpans.end();
+            // this.embeddedEnd = this.embeddedSpans.end();
 
             // Case 6
             // |---|
@@ -1138,7 +1146,6 @@
 
         public short elementRef = -1;
 
-
         public void clear () {
             this.start = -1;
             this.end = -1;
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAlterQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAlterQueryWrapper.java
index 6503b3f..a7b55cf 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAlterQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAlterQueryWrapper.java
@@ -103,22 +103,39 @@
     };
 
 
+    /*
+     * The query is extended to right in case one alternative is extended to the right.
+     */
     @Override
-    public SpanQuery toQuery () throws QueryException {
+    public boolean isExtendedToTheRight () {
+        if (this.alternatives.size() == 0)
+            return this.alternatives.get(0).isExtendedToTheRight();
+        Iterator<SpanQueryWrapper> clause = this.alternatives.iterator();
+        while (clause.hasNext()) {
+            if (clause.next().isExtendedToTheRight()) {
+                return true;
+            };
+        };
+        return false;
+    };
+
+
+    @Override
+    public SpanQuery toFragmentQuery () throws QueryException {
         if (this.isNull || this.alternatives.size() == 0)
             return (SpanQuery) null;
 
         if (this.alternatives.size() == 1) {
             return (SpanQuery) this.alternatives.get(0)
-                    .retrieveNode(this.retrieveNode).toQuery();
+                    .retrieveNode(this.retrieveNode).toFragmentQuery();
         };
 
         Iterator<SpanQueryWrapper> clause = this.alternatives.iterator();
         SpanOrQuery soquery = new SpanOrQuery(clause.next()
-                .retrieveNode(this.retrieveNode).toQuery());
+                .retrieveNode(this.retrieveNode).toFragmentQuery());
         while (clause.hasNext()) {
             soquery.addClause(clause.next().retrieveNode(this.retrieveNode)
-                    .toQuery());
+                    .toFragmentQuery());
         };
         return (SpanQuery) soquery;
     };
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAttributeQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAttributeQueryWrapper.java
index 968d544..5f11c02 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAttributeQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAttributeQueryWrapper.java
@@ -37,11 +37,11 @@
 
 
     @Override
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
         if (isNull || isEmpty)
             return null;
 
-        SpanQuery sq = subquery.retrieveNode(this.retrieveNode).toQuery();
+        SpanQuery sq = subquery.retrieveNode(this.retrieveNode).toFragmentQuery();
         if (sq == null) {
             isNull = true;
             return null;
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanClassQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanClassQueryWrapper.java
index 05edf76..51d4ab7 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanClassQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanClassQueryWrapper.java
@@ -66,12 +66,12 @@
     };
 
 
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
         if (this.subquery.isNull())
             return (SpanQuery) null;
 
         SpanQuery sq = (SpanQuery) this.subquery
-                .retrieveNode(this.retrieveNode).toQuery();
+                .retrieveNode(this.retrieveNode).toFragmentQuery();
 
         if (sq == null)
             return (SpanQuery) null;
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanElementQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanElementQueryWrapper.java
index bb9ddc2..3112dc2 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanElementQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanElementQueryWrapper.java
@@ -26,7 +26,7 @@
 
 
     @Override
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
         // Todo: Respect request for retrieving node data (i.e. depth information)
         return (SpanQuery) new SpanElementQuery(this.field, this.element);
     };
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanFocusQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanFocusQueryWrapper.java
index adcce08..8fadb3e 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanFocusQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanFocusQueryWrapper.java
@@ -49,11 +49,11 @@
     };
 
 
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
         if (this.subquery.isNull())
             return (SpanQuery) null;
         return new SpanFocusQuery(this.subquery.retrieveNode(this.retrieveNode)
-                .toQuery(), this.number);
+                .toFragmentQuery(), this.number);
     };
 
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQueryWrapper.java
index 987d21a..2cb72fc 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQueryWrapper.java
@@ -1,8 +1,10 @@
 package de.ids_mannheim.korap.query.wrap;
 
 import org.apache.lucene.search.spans.SpanQuery;
-
 import de.ids_mannheim.korap.util.QueryException;
+import de.ids_mannheim.korap.query.SpanFocusQuery;
+import de.ids_mannheim.korap.query.SpanClassQuery;
+import de.ids_mannheim.korap.query.SpanWithinQuery;
 
 // TODO: Add warnings and errors - using KrillQuery
 
@@ -43,11 +45,37 @@
      * @return A {@link SpanQuery} object.
      * @throws QueryException
      */
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
+        System.err.println("||||||||||||||||||||||||||");
         return (SpanQuery) null;
     };
 
 
+
+    /**
+     * Serialize the wrapped query and return a SpanQuery.
+     * This will be the final query and may be rewritten.
+     * 
+     * @return A {@link SpanQuery} object.
+     * @throws QueryException
+     */
+    public SpanQuery toQuery () throws QueryException {
+
+        if (this.isNull() || this.isEmpty())
+            return null;
+
+        // Wrap the query in a <base/s=t>, if it's extended to the right
+        if (this.isExtendedToTheRight()) {
+            return new SpanFocusQuery(
+                new SpanWithinQuery(
+                    "base/s:t",
+                    new SpanClassQuery(this.toFragmentQuery(), (byte) 254)), (byte) 254
+            );
+        };
+
+        return this.toFragmentQuery();
+    };
+
     /**
      * Boolean value indicating that the wrapped query
      * is optional.
@@ -149,6 +177,13 @@
     };
 
 
+    public SpanQueryWrapper isExtended (boolean extended) {
+        this.isExtended = extended;
+        return this;
+    };
+
+
+
     /**
      * Boolean value indicating that the wrapped query
      * is extended by a subquery to the right.
@@ -172,6 +207,12 @@
     };
 
 
+    public SpanQueryWrapper isExtendedToTheRight (boolean extended) {
+        this.isExtendedToTheRight = extended;
+        return this;
+    };
+
+
     /**
      * Check, if the wrapped query can be used as an
      * anchor query in a sequence, i.e. a query that
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanReferenceQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanReferenceQueryWrapper.java
index 8b4c54d..48ba7c2 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanReferenceQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanReferenceQueryWrapper.java
@@ -34,13 +34,13 @@
 
 
     @Override
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
 
         if (this.isNull() || this.isEmpty()) {
             return null;
         }
 
-        SpanQuery sq = subQuery.retrieveNode(this.retrieveNode).toQuery();
+        SpanQuery sq = subQuery.retrieveNode(this.retrieveNode).toFragmentQuery();
         if (sq == null)
             return null;
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRegexQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRegexQueryWrapper.java
index 24317ad..30ea49b 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRegexQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRegexQueryWrapper.java
@@ -47,7 +47,7 @@
     };
 
 
-    public SpanQuery toQuery () {
+    public SpanQuery toFragmentQuery () {
         return this.query;
     };
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRelationWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRelationWrapper.java
index 992787c..3eea9c8 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRelationWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRelationWrapper.java
@@ -37,14 +37,14 @@
 
 
     @Override
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
 
         if (this.isNull() || this.isEmpty()) {
             return null;
         }
 
         SpanTermQuery relationTermQuery = (SpanTermQuery) relationQuery
-                .retrieveNode(this.retrieveNode).toQuery();
+                .retrieveNode(this.retrieveNode).toFragmentQuery();
         if (relationTermQuery == null)
             return null;
 
@@ -54,7 +54,7 @@
         if (subQuery1.isEmpty) {
             if (!subQuery2.isEmpty) {
                 // match target
-                subq2 = subQuery2.retrieveNode(this.retrieveNode).toQuery();
+                subq2 = subQuery2.retrieveNode(this.retrieveNode).toFragmentQuery();
                 if (subQuery1.hasClass) {
                     rq.setSourceClass(subQuery1.getClassNumber());
                 }
@@ -65,7 +65,7 @@
         else if (subQuery2.isEmpty) {
             if (!subQuery1.isEmpty) {
                 // match source
-                subq1 = subQuery1.retrieveNode(this.retrieveNode).toQuery();
+                subq1 = subQuery1.retrieveNode(this.retrieveNode).toFragmentQuery();
                 if (subQuery2.hasClass) {
                     rq.setTargetClass(subQuery2.getClassNumber());
                 }
@@ -74,8 +74,8 @@
         }
         else {
             // match both
-            subq1 = subQuery1.retrieveNode(this.retrieveNode).toQuery();
-            subq2 = subQuery2.retrieveNode(this.retrieveNode).toQuery();
+            subq1 = subQuery1.retrieveNode(this.retrieveNode).toFragmentQuery();
+            subq2 = subQuery2.retrieveNode(this.retrieveNode).toFragmentQuery();
             return new SpanRelationMatchQuery(rq, subq1, subq2, true);
         }
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRepetitionQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRepetitionQueryWrapper.java
index 3f78fab..dbbdd67 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRepetitionQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRepetitionQueryWrapper.java
@@ -83,7 +83,8 @@
 
 
     // Serialize to Lucene SpanQuery
-    public SpanQuery toQuery () throws QueryException {
+    @Override
+    public SpanQuery toFragmentQuery () throws QueryException {
 
         // The query is null
         if (this.isNull)
@@ -96,11 +97,11 @@
 
         // The query is not a repetition query at all, but may be optional
         if (this.min == 1 && this.max == 1)
-            return this.subquery.retrieveNode(this.retrieveNode).toQuery();
+            return this.subquery.retrieveNode(this.retrieveNode).toFragmentQuery();
 
         // That's a fine repetition query
         return new SpanRepetitionQuery(this.subquery.retrieveNode(
-                this.retrieveNode).toQuery(), this.min, this.max, true);
+                this.retrieveNode).toFragmentQuery(), this.min, this.max, true);
     };
 
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSegmentQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSegmentQueryWrapper.java
index f89c5b7..1b1929b 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSegmentQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSegmentQueryWrapper.java
@@ -195,7 +195,7 @@
     };
 
 
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
         if (this.isNull || (this.inclusive.size() + this.exclusive.size() == 0)) {
             return (SpanQuery) null;
         }
@@ -219,10 +219,10 @@
 
     private SpanQuery _listToQuery (ArrayList<SpanQueryWrapper> list)
             throws QueryException {
-        SpanQuery query = list.get(0).toQuery();
+        SpanQuery query = list.get(0).toFragmentQuery();
 
         for (int i = 1; i < list.size(); i++) {
-            query = new SpanSegmentQuery(query, list.get(i).toQuery());
+            query = new SpanSegmentQuery(query, list.get(i).toFragmentQuery());
         };
 
         return (SpanQuery) query;
@@ -232,13 +232,13 @@
     private SpanQuery _listToOrQuery (ArrayList<SpanQueryWrapper> list)
             throws QueryException {
         if (list.size() == 1) {
-            return (SpanQuery) list.get(0).toQuery();
+            return (SpanQuery) list.get(0).toFragmentQuery();
         };
 
         Iterator<SpanQueryWrapper> clause = list.iterator();
-        SpanOrQuery soquery = new SpanOrQuery(clause.next().toQuery());
+        SpanOrQuery soquery = new SpanOrQuery(clause.next().toFragmentQuery());
         while (clause.hasNext()) {
-            soquery.addClause(clause.next().toQuery());
+            soquery.addClause(clause.next().toFragmentQuery());
         };
         return (SpanQuery) soquery;
     };
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSequenceQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSequenceQueryWrapper.java
index 2ef20f0..eadba22 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSequenceQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSequenceQueryWrapper.java
@@ -19,7 +19,6 @@
 /*
   TODO: Make isNegative work!
   TODO: Make isEmpty work!
-  TODO: Make isExtendedToTheRight work!
   TODO: Evaluate if spanNext(spanNext(a,b),spanNext(c,d)) is faster
         than spanNext(spanNext(spanNext(a,b),c),d)
   TODO: Improve support for SpanElementQueryWrapper in constraints!
@@ -135,7 +134,7 @@
         if (DEBUG) {
             if (!sswq.isEmpty()) {
                 try {
-                    log.trace("New span sequence {}", sswq.toQuery().toString());
+                    log.trace("New span sequence {}", sswq.toFragmentQuery().toString());
                 }
                 catch (QueryException qe) {
                     log.trace("Unable to serialize query {}", qe.getMessage());
@@ -547,7 +546,7 @@
         // Todo: This should possibly be evaluated to a query later on!
         try {
             this.constraints.add(new DistanceConstraint((SpanElementQuery) unit
-                    .retrieveNode(this.retrieveNode).toQuery(), min, max,
+                    .retrieveNode(this.retrieveNode).toFragmentQuery(), min, max,
                     isInOrder, exclusion));
         }
         catch (QueryException qe) {
@@ -638,11 +637,18 @@
         return super.isNegative();
     };
 
-
     public boolean isExtendedToTheRight () {
         if (!this.isSolved)
             _solveProblematicSequence();
-        return this.isExtendedToTheRight;
+
+        int size = this.segments.size();
+
+        // Nothing to do
+        if (size == 0 || this.isNull())
+            return false;
+
+        // Return the value of the last segment
+        return this.segments.get(size - 1).isExtendedToTheRight();
     };
 
 
@@ -652,7 +658,7 @@
      * @return A {@link SpanQuery} object.
      * @throws QueryException
      */
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
 
         // There was a serialization failure not yet reported
         if (this.constraintException != null)
@@ -676,7 +682,7 @@
             // Unproblematic single query
             if (this.segments.get(0).maybeAnchor())
                 return (SpanQuery) this.segments.get(0)
-                        .retrieveNode(this.retrieveNode).toQuery();
+                        .retrieveNode(this.retrieveNode).toFragmentQuery();
 
             if (this.segments.get(0).isEmpty())
                 throw new QueryException(613,
@@ -708,11 +714,11 @@
         };
 
         // Create the initial query
-        SpanQuery query = null;// = this.segments.get(0).toQuery();
+        SpanQuery query = null;
         int i = 0;
         while (query == null && i < this.segments.size()) {
             query = this.segments.get(i).retrieveNode(this.retrieveNode)
-                    .toQuery();
+                    .toFragmentQuery();
             i++;
         };
 
@@ -724,7 +730,7 @@
             for (; i < this.segments.size(); i++) {
 
                 SpanQuery second = this.segments.get(i)
-                        .retrieveNode(this.retrieveNode).toQuery();
+                        .retrieveNode(this.retrieveNode).toFragmentQuery();
                 if (second == null)
                     continue;
 
@@ -746,7 +752,7 @@
                         throw new QueryException(613, limitationError);
 
                     SpanQuery sq = (SpanQuery) this.segments.get(i)
-                            .retrieveNode(this.retrieveNode).toQuery();
+                            .retrieveNode(this.retrieveNode).toFragmentQuery();
                     if (sq == null)
                         continue;
 
@@ -765,7 +771,7 @@
                         throw new QueryException(613, limitationError);
 
                     SpanQuery sq = (SpanQuery) this.segments.get(i)
-                            .retrieveNode(this.retrieveNode).toQuery();
+                            .retrieveNode(this.retrieveNode).toFragmentQuery();
                     if (sq == null)
                         continue;
 
@@ -786,7 +792,7 @@
                 throw new QueryException(613, limitationError);
 
             SpanQuery sq = (SpanQuery) this.segments.get(i)
-                    .retrieveNode(this.retrieveNode).toQuery();
+                    .retrieveNode(this.retrieveNode).toFragmentQuery();
             if (sq == null)
                 continue;
 
@@ -797,7 +803,6 @@
     };
 
 
-
     /*
       Check if there are problematic segments in the sequence
       (either negative, optional or empty) and deal with them
@@ -938,11 +943,17 @@
                         problem.getClassNumber());
 
             query = new SpanExpansionQuery(anchor.retrieveNode(
-                                                               this.retrieveNode).toQuery(), problem.isOptional() ? 0 : problem.getMin(),
+                                                               this.retrieveNode).toFragmentQuery(), problem.isOptional() ? 0 : problem.getMin(),
                     problem.getMax(), direction,
                     problem.hasClass() ? problem.getClassNumber() : (byte) 0,
                     true);
-            return new SpanSimpleQueryWrapper(query).isExtended(true);
+            SpanQueryWrapper sqw = new SpanSimpleQueryWrapper(query).isExtended(true);
+
+            // Set right extension
+            if (direction >= 0)
+                sqw.isExtendedToTheRight(true);
+
+            return sqw;
         }
 
         // make negative extension to anchor
@@ -955,12 +966,19 @@
                         problem.getClassNumber());
 
             query = new SpanExpansionQuery(anchor.retrieveNode(
-                    this.retrieveNode).toQuery(), problem.retrieveNode(
-                    this.retrieveNode).toQuery(), problem.getMin(),
+                    this.retrieveNode).toFragmentQuery(), problem.retrieveNode(
+                    this.retrieveNode).toFragmentQuery(), problem.getMin(),
                     problem.getMax(), direction,
                     problem.hasClass() ? problem.getClassNumber() : (byte) 0,
                     true);
-            return new SpanSimpleQueryWrapper(query).isExtended(true);
+
+            SpanQueryWrapper sqw = new SpanSimpleQueryWrapper(query).isExtended(true);
+
+            // Set right extension
+            if (direction >= 0)
+                sqw.isExtendedToTheRight(true);
+
+            return sqw;
         };
 
         if (DEBUG)
@@ -979,11 +997,11 @@
         // [base=der][base=baum]
         if (mergeLeft)
             ssqw.append(new SpanSimpleQueryWrapper(problem.retrieveNode(
-                    this.retrieveNode).toQuery()));
+                    this.retrieveNode).toFragmentQuery()));
         // [base=baum][base=der]
         else
             ssqw.prepend(new SpanSimpleQueryWrapper(problem.retrieveNode(
-                    this.retrieveNode).toQuery()));
+                    this.retrieveNode).toFragmentQuery()));
 
         saqw.or(ssqw);
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSimpleQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSimpleQueryWrapper.java
index 28edbf6..61608e5 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSimpleQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSimpleQueryWrapper.java
@@ -26,13 +26,8 @@
     };
 
 
-    public SpanQuery toQuery () {
+    @Override
+    public SpanQuery toFragmentQuery () {
         return this.query;
     };
-
-
-    public SpanSimpleQueryWrapper isExtended (boolean extended) {
-        this.isExtended = true;
-        return this;
-    };
 };
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSubspanQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSubspanQueryWrapper.java
index ec61e70..8c809c5 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSubspanQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSubspanQueryWrapper.java
@@ -105,12 +105,12 @@
 
 
     @Override
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
         if (this.isNull()) {
             return null;
         }
 
-        SpanQuery sq = subquery.retrieveNode(this.retrieveNode).toQuery();
+        SpanQuery sq = subquery.retrieveNode(this.retrieveNode).toFragmentQuery();
         if (sq == null)
             return null;
         if (sq instanceof SpanTermQuery) {
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWildcardQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWildcardQueryWrapper.java
index 6eb6bdf..5ac346e 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWildcardQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWildcardQueryWrapper.java
@@ -30,7 +30,7 @@
     };
 
 
-    public SpanQuery toQuery () {
+    public SpanQuery toFragmentQuery () {
         return this.query;
     };
 
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithAttributeQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithAttributeQueryWrapper.java
index b51557d..435b3ee 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithAttributeQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithAttributeQueryWrapper.java
@@ -118,7 +118,7 @@
 
 
     @Override
-    public SpanQuery toQuery () throws QueryException {
+    public SpanQuery toFragmentQuery () throws QueryException {
 
         if (isNull || isEmpty)
             return null;
@@ -134,7 +134,7 @@
     private SpanQuery createSpecificSpanWithAttributeQuery ()
             throws QueryException {
         SimpleSpanQuery withIdQuery = (SimpleSpanQuery) withIdQueryWrapper
-                .toQuery();
+                .toFragmentQuery();
         if (withIdQuery == null) {
             isNull = true;
             return null;
@@ -169,7 +169,7 @@
 
     private SpanAttributeQuery createSpanAttributeQuery (
             SpanQueryWrapper attrQueryWrapper) throws QueryException {
-        SpanQuery sq = attrQueryWrapper.toQuery();
+        SpanQuery sq = attrQueryWrapper.toFragmentQuery();
         if (sq != null) {
             if (sq instanceof SpanAttributeQuery)
                 return (SpanAttributeQuery) sq;
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithinQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithinQueryWrapper.java
index 904d771..89d1fc5 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithinQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithinQueryWrapper.java
@@ -88,16 +88,16 @@
             this.isNull = false;
     };
 
-
-    public SpanQuery toQuery () throws QueryException {
+    @Override
+    public SpanQuery toFragmentQuery () throws QueryException {
         if (this.isNull)
             return (SpanQuery) null;
 
         // TODO: if (wrap.isNegative())
 
         return new SpanWithinQuery(this.element.retrieveNode(this.retrieveNode)
-                .toQuery(),
-                this.wrap.retrieveNode(this.retrieveNode).toQuery(), this.flag);
+                .toFragmentQuery(),
+                this.wrap.retrieveNode(this.retrieveNode).toFragmentQuery(), this.flag);
     };
 
 
diff --git a/src/main/java/de/ids_mannheim/korap/response/Match.java b/src/main/java/de/ids_mannheim/korap/response/Match.java
index 0ed45b2..922f9ae 100644
--- a/src/main/java/de/ids_mannheim/korap/response/Match.java
+++ b/src/main/java/de/ids_mannheim/korap/response/Match.java
@@ -24,6 +24,7 @@
 import com.fasterxml.jackson.databind.node.ArrayNode;
 import com.fasterxml.jackson.databind.node.ObjectNode;
 
+import static de.ids_mannheim.korap.util.KrillByte.*;
 import de.ids_mannheim.korap.index.AbstractDocument;
 import de.ids_mannheim.korap.index.PositionsToOffset;
 import de.ids_mannheim.korap.query.SpanElementQuery;
@@ -232,11 +233,10 @@
 
                 // Todo element searches!
 
-                // Highlights!
+                // Highlights! This is a class PTI
                 if (b[0] == 0) {
                     bb.put(b);
                     bb.position(1); // Ignore PTI
-
                     int start = bb.getInt();
                     int end = bb.getInt();
                     byte number = bb.get();
@@ -244,18 +244,18 @@
                     if (DEBUG)
                         log.trace(
                                 "Have a highlight of class {} in {}-{} inside of {}-{}",
-                                number, start, end, this.getStartPos(),
+                                unsignedByte(number), start, end, this.getStartPos(),
                                 this.getEndPos());
 
                     // Ignore classes out of match range and set by the system
                     // TODO: This may be decidable by PTI!
-                    if ((number & 0xFF) <= 128 && start >= this.getStartPos()
+                    if (unsignedByte(number) <= 128 && start >= this.getStartPos()
                             && end <= this.getEndPos()) {
-                        log.trace("Add highlight of class {}!", number);
+                        log.trace("Add highlight of class {}!", unsignedByte(number));
                         this.addHighlight(start, end - 1, number);
                     }
                     else if (DEBUG) {
-                        log.trace("Don't add highlight of class {}!", number);
+                        log.trace("Don't add highlight of class {}!", unsignedByte(number));
                     };
                 }
 
@@ -1027,7 +1027,10 @@
     };
 
 
-
+    /*
+     * This takes a clean string and the tag stack
+     * to decorate the string with annotations.
+     */
     private void _processHighlightSnippet (String clean, ArrayList<int[]> stack) {
 
         if (DEBUG)
@@ -1037,7 +1040,10 @@
 
         this.snippetArray = new HighlightCombinator();
 
+        // Iterate over all elements of the stack
         for (int[] element : stack) {
+
+            // The position
             pos = element[3] != 0 ? element[0] : element[1];
 
             if (pos > oldPos) {
diff --git a/src/main/java/de/ids_mannheim/korap/util/KrillByte.java b/src/main/java/de/ids_mannheim/korap/util/KrillByte.java
index 419a43f..0666cc7 100644
--- a/src/main/java/de/ids_mannheim/korap/util/KrillByte.java
+++ b/src/main/java/de/ids_mannheim/korap/util/KrillByte.java
@@ -62,4 +62,15 @@
             number |= (data[offset - i] & 0xff) << (i << 3);
         return number;
     };
+
+
+    /**
+     * Return the unsigned value of a byte.
+     * 
+     * @param data
+     *            The byte.
+     */ 
+    public static int unsignedByte (byte data) {
+        return (int) data & 0xFF;
+    };
 };
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
index c618521..9b8e374 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestSpanExpansionIndex.java
@@ -329,6 +329,8 @@
         ki.addDoc(createFieldDoc2()); // only main clause
         ki.commit();
 
+        // Warning: This is not armoured by <base/s=t>!
+
         // See /queries/bugs/repetition_group_rewrite
         // spanRepetition(spanExpansion(
         //     SpanMultiTermQueryWrapper(tokens:/cnx/p:A/), []{1, 1}, right){2,2}
@@ -341,13 +343,19 @@
         SpanRepetitionQuery rep = new SpanRepetitionQuery(seq, 2, 2, true);
 
         kr = ki.search(rep, (short) 20);
+
+        for (Match km : kr.getMatches()){
+            System.out.println(
+                               km.getStartPos() +
+                               "," +
+                               km.getEndPos() +
+                               " " +
+                               km.getSnippetBrackets()
+                               );
+        };
+
         assertEquals((long) 3, kr.getTotalResults());
 
-        /*
-         * for (Match km : kr.getMatches()){
-         * System.out.println(km.getStartPos() +","+km.getEndPos()+" "
-         * +km.getSnippetBrackets() ); }
-         */
     }
 
 
@@ -361,9 +369,8 @@
     @Test
     public void testExpansionQueryBug3 () throws IOException, QueryException {
         KrillIndex ki = new KrillIndex();
-        ki.addDoc(createFieldDoc0()); // same doc
-        ki.addDoc(createFieldDoc1()); // only not clause
-        ki.addDoc(createFieldDoc2()); // only main clause
+        ki.addDoc(createFieldDoc3());
+        ki.addDoc(createFieldDoc4());
         ki.commit();
 
         String json = readFile(getClass().getResource(
@@ -371,22 +378,23 @@
         );
         KrillQuery kq = new KrillQuery("base");
         SpanQuery sq = kq.fromJson(json).toQuery();
-        /*
-        assertEquals(sq.toString(),
-                     "spanExpansion(base:s:a, []{0, 100}, right)");
-        */
-        kr = ki.search(sq, (short) 11);
-        assertEquals((long) 1111, kr.getTotalResults());
+        assertEquals(sq.toString(),"focus(254: spanContain(<base:base/s:t />, {254: spanExpansion(base:s:c, []{0, 4}, right)}))");
 
-        assertEquals("[c]eccecd ...",kr.getMatch(0).getSnippetBrackets());
-        assertEquals("[ce]ccecde ...",kr.getMatch(1).getSnippetBrackets());
-        assertEquals("",kr.getMatch(10).getSnippetBrackets());
-        assertEquals("",kr.getMatch(11).getSnippetBrackets());
-        /*
-        assertEquals("[ce]cceecde",kr.getMatch(1).getSnippetBrackets());
-        assertEquals("[cecceecde]",kr.getMatch(10).getSnippetBrackets());
-        assertEquals("[bb]ccdd",kr.getMatch(11).getSnippetBrackets());
-        */
+        kr = ki.search(sq, (short) 10);
+
+        assertEquals("[c]ab",kr.getMatch(0).getSnippetBrackets());
+        assertEquals("[ca]b",kr.getMatch(1).getSnippetBrackets());
+        assertEquals("[cab]",kr.getMatch(2).getSnippetBrackets());
+        assertEquals("[c]e",kr.getMatch(3).getSnippetBrackets());
+        assertEquals("[ce]",kr.getMatch(4).getSnippetBrackets());
+        assertEquals(5, kr.getTotalResults());
+
+        sq = kq.builder().tag("base/s:t").toQuery();
+        assertEquals(sq.toString(),"<base:base/s:t />");
+        kr = ki.search(sq, (short) 5);
+        assertEquals("[cab]", kr.getMatch(0).getSnippetBrackets());
+        assertEquals("[ce]", kr.getMatch(1).getSnippetBrackets());
+        assertEquals(2, kr.getTotalResults());
     }
 
 
@@ -395,12 +403,17 @@
     private FieldDocument createFieldDoc0 () {
         FieldDocument fd = new FieldDocument();
         fd.addString("ID", "doc-0");
-        fd.addTV("base", "ceccecdeec", "[(0-1)s:c|_0$<i>0<i>1]"
-                + "[(1-2)s:e|_1$<i>1<i>2]" + "[(2-3)s:c|_2$<i>2<i>3]"
-                + "[(3-4)s:c|s:d|_3$<i>3<i>4]" + "[(4-5)s:e|_4$<i>4<i>5]"
-                + "[(5-6)s:c|_5$<i>5<i>6]" + "[(6-7)s:d|_6$<i>6<i>7]"
-                + "[(7-8)s:e|_7$<i>7<i>8]" + "[(8-9)s:e|_8$<i>8<i>9]"
-                + "[(9-10)s:c|_9$<i>9<i>10]");
+        fd.addTV("base", "ceccecdeec",
+                 "[(0-1)s:c|_0$<i>0<i>1|<>:base/s:t$<b>64<i>0<i>10<i>10<b>0]"
+                 + "[(1-2)s:e|_1$<i>1<i>2]"
+                 + "[(2-3)s:c|_2$<i>2<i>3]"
+                 + "[(3-4)s:c|s:d|_3$<i>3<i>4]"
+                 + "[(4-5)s:e|_4$<i>4<i>5]"
+                 + "[(5-6)s:c|_5$<i>5<i>6]"
+                 + "[(6-7)s:d|_6$<i>6<i>7]"
+                 + "[(7-8)s:e|_7$<i>7<i>8]"
+                 + "[(8-9)s:e|_8$<i>8<i>9]"
+                 + "[(9-10)s:c|_9$<i>9<i>10]");
         return fd;
     }
 
@@ -408,10 +421,13 @@
     private FieldDocument createFieldDoc1 () {
         FieldDocument fd = new FieldDocument();
         fd.addString("ID", "doc-1");
-        fd.addTV("base", "bbccdd", "[(0-1)s:b|s:c|_0$<i>0<i>1]"
-                + "[(1-2)s:b|_1$<i>1<i>2]" + "[(2-3)s:c|_2$<i>2<i>3]"
-                + "[(3-4)s:c|_3$<i>3<i>4]" + "[(4-5)s:d|_4$<i>4<i>5]"
-                + "[(5-6)s:d|_5$<i>5<i>6]");
+        fd.addTV("base", "bbccdd",
+                 "[(0-1)s:b|_0$<i>0<i>1|<>:base/s:t$<b>64<i>0<i>6<i>6<b>0]]"
+                 + "[(1-2)s:b|_1$<i>1<i>2]"
+                 + "[(2-3)s:c|_2$<i>2<i>3]"
+                 + "[(3-4)s:c|_3$<i>3<i>4]"
+                 + "[(4-5)s:d|_4$<i>4<i>5]"
+                 + "[(5-6)s:d|_5$<i>5<i>6]");
         return fd;
     }
 
@@ -419,10 +435,34 @@
     private FieldDocument createFieldDoc2 () {
         FieldDocument fd = new FieldDocument();
         fd.addString("ID", "doc-2");
-        fd.addTV("base", "beccea", "[(0-1)s:b|s:c|_0$<i>0<i>1]"
-                + "[(1-2)s:e|_1$<i>1<i>2]" + "[(2-3)s:c|_2$<i>2<i>3]"
-                + "[(3-4)s:c|_3$<i>3<i>4]" + "[(4-5)s:e|_4$<i>4<i>5]"
-                + "[(5-6)s:a|_5$<i>5<i>6]");
+        fd.addTV("base", "beccea",
+                 "[(0-1)s:b|_0$<i>0<i>1|<>:base/s:t$<b>64<i>0<i>6<i>6<b>0]]"
+                 + "[(1-2)s:e|_1$<i>1<i>2]"
+                 + "[(2-3)s:c|_2$<i>2<i>3]"
+                 + "[(3-4)s:c|_3$<i>3<i>4]"
+                 + "[(4-5)s:e|_4$<i>4<i>5]"
+                 + "[(5-6)s:a|_5$<i>5<i>6]");
+        return fd;
+    }
+
+
+    private FieldDocument createFieldDoc3 () {
+        FieldDocument fd = new FieldDocument();
+        fd.addString("ID", "doc-3");
+        fd.addTV("base", "cab",
+                 "[(0-1)s:c|_0$<i>0<i>1|<>:base/s:t$<b>64<i>0<i>3<i>3<b>0]]"
+                 + "[(1-2)s:a|_1$<i>1<i>2]"
+                 + "[(2-3)s:b|_2$<i>2<i>3]");
+        return fd;
+    }
+
+
+    private FieldDocument createFieldDoc4 () {
+        FieldDocument fd = new FieldDocument();
+        fd.addString("ID", "doc-3");
+        fd.addTV("base", "ce",
+                 "[(0-1)s:c|_0$<i>0<i>1|<>:base/s:t$<b>64<i>0<i>2<i>2<b>0]]"
+                 + "[(1-2)s:e|_1$<i>1<i>2]");
         return fd;
     }
 
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestWithinIndex.java b/src/test/java/de/ids_mannheim/korap/index/TestWithinIndex.java
index 78d623c..cbe6a87 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestWithinIndex.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestWithinIndex.java
@@ -854,12 +854,14 @@
         // Case 1, 6, 7, 13
         // xy<a><a>x</a>b<a>c</a></a>x
         FieldDocument fd = new FieldDocument();
-        fd.addTV("base", "x  y  x  b  c  x  ", "[(0-3)s:x|_0$<i>0<i>3]"
-                + "[(3-6)s:y|_1$<i>3<i>6]"
-                + "[(6-9)s:x|_2$<i>6<i>9|<>:a$<b>64<i>6<i>9<i>3<b>0|"
-                + "<>:a$<b>64<i>6<i>15<i>5<b>0]" + "[(9-12)s:b|_3$<i>9<i>12]"
-                + "[(12-15)s:c|_4$<i>12<i>15|<>:a$<b>64<i>12<i>15<i>5<b>0]"
-                + "[(15-18)s:x|_5$<i>15<i>18]");
+        fd.addTV("base", "x  y  x  b  c  x  ",
+                 "[(0-3)s:x|_0$<i>0<i>3]"
+                 + "[(3-6)s:y|_1$<i>3<i>6]"
+                 + "[(6-9)s:x|_2$<i>6<i>9|<>:a$<b>64<i>6<i>9<i>3<b>0|"
+                 + "<>:a$<b>64<i>6<i>15<i>5<b>0]" +
+                 "[(9-12)s:b|_3$<i>9<i>12]"
+                 + "[(12-15)s:c|_4$<i>12<i>15|<>:a$<b>64<i>12<i>15<i>5<b>0]"
+                 + "[(15-18)s:x|_5$<i>15<i>18]");
         ki.addDoc(fd);
 
         // Save documents
@@ -870,9 +872,14 @@
         SpanQuery sq = new SpanWithinQuery(new SpanElementQuery("base", "a"),
                 new SpanTermQuery(new Term("base", "s:x")));
 
+        assertEquals("spanContain(<base:a />, base:s:x)", sq.toString());
         Result kr = ki.search(sq, (short) 10);
 
         assertEquals("totalResults", kr.getTotalResults(), 2);
+
+        assertEquals("x  y  [x  ]b  c  x  ", kr.getMatch(0).getSnippetBrackets());
+        assertEquals("x  y  [x  b  c  ]x  ", kr.getMatch(1).getSnippetBrackets());
+
         assertEquals("StartPos (0)", 2, kr.getMatch(0).startPos);
         assertEquals("EndPos (0)", 3, kr.getMatch(0).endPos);
         assertEquals("StartPos (1)", 2, kr.getMatch(1).startPos);
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestKrillQuery.java b/src/test/java/de/ids_mannheim/korap/query/TestKrillQuery.java
index e379301..4c1776b 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestKrillQuery.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestKrillQuery.java
@@ -309,7 +309,7 @@
     public void KorapSequenceWithEmptyRepetitionQuery () throws QueryException {
         QueryBuilder kq = new QueryBuilder("field");
         SpanQuery sq = kq.seq(kq.seg("try")).append(kq.repeat(kq.empty(),0,100)).toQuery();
-        assertEquals("spanExpansion(field:try, []{0, 100}, right)",sq.toString());
+        assertEquals("focus(254: spanContain(<field:base/s:t />, {254: spanExpansion(field:try, []{0, 100}, right)}))",sq.toString());
     };
 
 
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
index d44a260..bbc0298 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestKrillQueryJSON.java
@@ -546,7 +546,7 @@
             KrillQuery kq = new KrillQuery("tokens");
 
             assertEquals(kq.fromJson(json).toQuery().toString(),
-                    "spanExpansion(tokens:s:c, []{0, 100}, right)");
+                    "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(tokens:s:c, []{0, 100}, right)}))");
         }
         catch (QueryException e) {
             fail(e.getMessage());
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestSpanSequenceQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestSpanSequenceQueryJSON.java
index 8762e4f..6b3285c 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestSpanSequenceQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestSpanSequenceQueryJSON.java
@@ -38,7 +38,7 @@
     public void queryJSONseqEmptyEnd () throws QueryException {
         SpanQueryWrapper sqwi = jsonQueryFile("empty-last.jsonld");
         assertEquals(sqwi.toQuery().toString(),
-                "spanExpansion(tokens:s:der, []{1, 1}, right)");
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(tokens:s:der, []{1, 1}, right)}))");
     };
 
 
@@ -47,7 +47,7 @@
         SpanQueryWrapper sqwi = jsonQueryFile("empty-last-class.jsonld");
         // der{3:[]}
         assertEquals(sqwi.toQuery().toString(),
-                "spanExpansion(tokens:s:der, []{1, 1}, right, class:3)");
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(tokens:s:der, []{1, 1}, right, class:3)}))");
     };
 
 
@@ -56,7 +56,7 @@
         SpanQueryWrapper sqwi = jsonQueryFile("empty-last-repetition.jsonld");
         // der[]{3,5}
         assertEquals(sqwi.toQuery().toString(),
-                "spanExpansion(tokens:s:der, []{3, 5}, right)");
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(tokens:s:der, []{3, 5}, right)}))");
     };
 
 
@@ -128,7 +128,7 @@
         SpanQueryWrapper sqwi = jsonQueryFile("empty-surround.jsonld");
         // [][tt/p=NN][]
         assertEquals(sqwi.toQuery().toString(),
-                "spanExpansion(spanExpansion(tokens:tt/p:NN, []{1, 1}, left), []{1, 1}, right)");
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(spanExpansion(tokens:tt/p:NN, []{1, 1}, left), []{1, 1}, right)}))");
     };
 
 
@@ -138,7 +138,7 @@
         // [][tt/p=NN]{2:[]}
         assertEquals(
                 sqwi.toQuery().toString(),
-                "spanExpansion(spanExpansion(tokens:tt/p:NN, []{1, 1}, left), []{1, 1}, right, class:2)");
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(spanExpansion(tokens:tt/p:NN, []{1, 1}, left), []{1, 1}, right, class:2)}))");
     };
 
 
@@ -148,7 +148,7 @@
         // {3:[]}[tt/p=NN]{2:[]}
         assertEquals(
                 sqwi.toQuery().toString(),
-                "spanExpansion(spanExpansion(tokens:tt/p:NN, []{1, 1}, left, class:3), []{1, 1}, right, class:2)");
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(spanExpansion(tokens:tt/p:NN, []{1, 1}, left, class:3), []{1, 1}, right, class:2)}))");
     };
 
 
@@ -157,7 +157,7 @@
         SpanQueryWrapper sqwi = jsonQueryFile("empty-surround-repetition.jsonld");
         // [][tt/p=NN][]{2,7}
         assertEquals(sqwi.toQuery().toString(),
-                "spanExpansion(spanExpansion(tokens:tt/p:NN, []{1, 1}, left), []{2, 7}, right)");
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(spanExpansion(tokens:tt/p:NN, []{1, 1}, left), []{2, 7}, right)}))");
     };
 
 
@@ -166,7 +166,7 @@
         SpanQueryWrapper sqwi = jsonQueryFile("empty-surround-repetition-2.jsonld");
         // []{3,5}[tt/p=NN][]{2,7}
         assertEquals(sqwi.toQuery().toString(),
-                "spanExpansion(spanExpansion(tokens:tt/p:NN, []{3, 5}, left), []{2, 7}, right)");
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(spanExpansion(tokens:tt/p:NN, []{3, 5}, left), []{2, 7}, right)}))");
     };
 
 
@@ -179,7 +179,7 @@
         // {1:[]{3,8}}[tt/p=NN]{2:[]}{2,7}
         assertEquals(
                 sqwi.toQuery().toString(),
-                "spanExpansion(spanExpansion(tokens:tt/p:NN, []{3, 8}, left, class:1), []{2, 7}, right, class:2)");
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(spanExpansion(tokens:tt/p:NN, []{3, 8}, left, class:1), []{2, 7}, right, class:2)}))");
     };
 
 
@@ -205,7 +205,7 @@
         SpanQueryWrapper sqwi = jsonQueryFile("negative-last.jsonld");
         // [tt/p=NN][tt/p!=NN]
         assertEquals(sqwi.toQuery().toString(),
-                "spanExpansion(tokens:tt/p:NN, !tokens:tt/p:NN{1, 1}, right)");
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(tokens:tt/p:NN, !tokens:tt/p:NN{1, 1}, right)}))");
     };
 
 
@@ -241,7 +241,7 @@
         // [tt/p=NN]{2:[tt/p!=NN]}
         SpanQuery sq = sqwi.toQuery();
         assertEquals(sq.toString(),
-                "spanExpansion(tokens:tt/p:NN, !tokens:tt/p:NN{1, 1}, right, class:2)");
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(tokens:tt/p:NN, !tokens:tt/p:NN{1, 1}, right, class:2)}))");
     };
 
 
@@ -250,7 +250,7 @@
         SpanQueryWrapper sqwi = jsonQueryFile("negative-last-class-repetition.jsonld");
         // [tt/p=NN]{2:[tt/p!=NN]{4,5}}
         assertEquals(sqwi.toQuery().toString(),
-                "spanExpansion(tokens:tt/p:NN, !tokens:tt/p:NN{4, 5}, right, class:2)");
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(tokens:tt/p:NN, !tokens:tt/p:NN{4, 5}, right, class:2)}))");
     };
 
 
@@ -260,7 +260,7 @@
         SpanQueryWrapper sqwi = jsonQueryFile("negative-last-class-repetition-2.jsonld");
         // [tt/p=NN]{2:[tt/p!=NN]}{4,5}
         assertEquals(sqwi.toQuery().toString(),
-                "spanExpansion(tokens:tt/p:NN, !tokens:tt/p:NN{4, 5}, right, class:2)");
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(tokens:tt/p:NN, !tokens:tt/p:NN{4, 5}, right, class:2)}))");
     };
 
 
@@ -284,7 +284,7 @@
         SpanQueryWrapper sqwi = jsonQueryFile("negative-last-sequence.jsonld");
         // [tt/p=NN]([tt/p!=DET][tt/p!=NN])
         assertEquals(
-                "spanExpansion(spanExpansion(tokens:tt/p:NN, !tokens:tt/p:DET{1, 1}, right), !tokens:tt/p:ADJ{1, 1}, right)",
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(spanExpansion(tokens:tt/p:NN, !tokens:tt/p:DET{1, 1}, right), !tokens:tt/p:ADJ{1, 1}, right)}))",
                 sqwi.toQuery().toString());
     };
 
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestSpanSubspanQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestSpanSubspanQueryJSON.java
index 273c5cd..d0db622 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestSpanSubspanQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestSpanSubspanQueryJSON.java
@@ -154,7 +154,7 @@
                 "/queries/submatch/embedded-valid-empty.jsonld").getFile();
         SpanQueryWrapper sqwi = getJSONQuery(filepath);
         SpanQuery sq = sqwi.toQuery();
-        assertEquals("spanExpansion(tokens:s:die, []{2, 5}, right)",
+        assertEquals("focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(tokens:s:die, []{2, 5}, right)}))",
                 sq.toString());
     }
 
@@ -178,7 +178,7 @@
         SpanQueryWrapper sqwi = getJSONQuery(filepath);
         SpanQuery sq = sqwi.toQuery();
         assertEquals(
-                "spanExpansion(tokens:s:das, !tokens:l:Baum{1, 1}, right)",
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(tokens:s:das, !tokens:l:Baum{1, 1}, right)}))",
                 sq.toString());
     }
 
@@ -191,7 +191,7 @@
         SpanQueryWrapper sqwi = getJSONQuery(filepath);
         SpanQuery sq = sqwi.toQuery();
         assertEquals(
-                "spanExpansion(tokens:s:das, !tokens:l:Baum{1, 1}, right, class:1)",
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(tokens:s:das, !tokens:l:Baum{1, 1}, right, class:1)}))",
                 sq.toString());
     }
 
@@ -245,7 +245,7 @@
         SpanQueryWrapper sqwi = getJSONQuery(filepath);
         SpanQuery sq = sqwi.toQuery();
         assertEquals(
-                "spanExpansion(tokens:s:das, !tokens:l:Baum{2, 2}, right)",
+                "focus(254: spanContain(<tokens:base/s:t />, {254: spanExpansion(tokens:s:das, !tokens:l:Baum{2, 2}, right)}))",
                 sq.toString());
     }
 }
diff --git a/src/test/resources/queries/bugs/expansion_bug_3.jsonld b/src/test/resources/queries/bugs/expansion_bug_3.jsonld
index 966dbd8..53f550f 100644
--- a/src/test/resources/queries/bugs/expansion_bug_3.jsonld
+++ b/src/test/resources/queries/bugs/expansion_bug_3.jsonld
@@ -23,9 +23,11 @@
 	],
 	"boundary":{
 	  "@type":"koral:boundary",
-	  "min":0
+	  "min":0,
+	  "max":4
 	},
-	"min":0
+	"min":0,
+	"max":4
       }
     ]
   },