Fix buffer rewind at end of transmission

Change-Id: I5abb90153931574acf0df5e95e5b286c883539e5
diff --git a/datok.go b/datok.go
index f804bf0..7e5a36d 100644
--- a/datok.go
+++ b/datok.go
@@ -951,6 +951,15 @@
 			}
 		}
 
+		if eot {
+			eot = false
+			textEnd = true
+			w.TextEnd(0)
+			if DEBUG {
+				fmt.Println("END OF TEXT")
+			}
+		}
+
 		// Rewind the buffer if necessary
 		if rewindBuffer {
 
@@ -976,15 +985,6 @@
 			}
 		}
 
-		if eot {
-			eot = false
-			textEnd = true
-			w.TextEnd(0)
-			if DEBUG {
-				fmt.Println("END OF TEXT")
-			}
-		}
-
 		// Move to representative state
 		if ta.isSeparate() {
 			t = ta.getBase()
diff --git a/matrix.go b/matrix.go
index 2ac31c3..3a06951 100644
--- a/matrix.go
+++ b/matrix.go
@@ -503,6 +503,16 @@
 			}
 		}
 
+		if eot {
+			eot = false
+			textEnd = true
+			w.TextEnd(buffc)
+			rewindBuffer = true
+			if DEBUG {
+				fmt.Println("END OF TEXT")
+			}
+		}
+
 		// Rewind the buffer if necessary
 		if rewindBuffer {
 
@@ -528,14 +538,6 @@
 			}
 		}
 
-		if eot {
-			eot = false
-			textEnd = true
-			w.TextEnd(buffc)
-			if DEBUG {
-				fmt.Println("END OF TEXT")
-			}
-		}
 		t &= ^FIRSTBIT
 
 		newchar = true
diff --git a/token_writer.go b/token_writer.go
index d75b261..9f4088a 100644
--- a/token_writer.go
+++ b/token_writer.go
@@ -48,6 +48,7 @@
 			// TODO:
 			//   Store in []uint16
 			//   and write to string
+
 			posC += offset
 			pos = append(pos, posC)
 			posC += len(buf) - offset
@@ -79,7 +80,7 @@
 			}
 			writer.WriteRune('\n')
 
-			posC = 0 - offset
+			posC = 0
 			pos = pos[:0]
 		}
 	} else {
diff --git a/token_writer_test.go b/token_writer_test.go
index 71524fb..291c3b9 100644
--- a/token_writer_test.go
+++ b/token_writer_test.go
@@ -49,4 +49,11 @@
 
 	matStr := w.String()
 	assert.Equal("This\n.\n\n0 4 4 5\nAnd\n.\n\n0 3 3 4\n", matStr)
+
+	w.Reset()
+	mat.TransduceTokenWriter(strings.NewReader("\nThis.\x0a\x04\nAnd.\n\x04\n"), tws)
+
+	matStr = w.String()
+	assert.Equal("This\n.\n\n1 5 5 6\nAnd\n.\n\n1 4 4 5\n", matStr)
+
 }