diff --git a/README.md b/README.md
index 38e3e3c..be17dcc 100644
--- a/README.md
+++ b/README.md
@@ -3,7 +3,7 @@ A Wiki in a box
 
 Pepperminty Wiki is a complete wiki contained in a single file, inspired by @am2064's [Minty Wiki](https://github.com/am2064/Minty-Wiki). It's open source too (under MPL-2.0), so contributions are welcome!
 
-**Latest Version:** v0.12 (stable) v0.13-dev (development)
+**Latest Version:** v0.12 (stable) v0.12.1-dev (development)
 
 ## Screenshots
 ![Main Page Example](https://i.imgur.com/8TokPXw.png)
diff --git a/build/index.php b/build/index.php
index 7122e90..d1d860e 100755
--- a/build/index.php
+++ b/build/index.php
@@ -305,7 +305,7 @@ Actions:
 ///////////////////////////////////////////////////////////////////////////////////////////////
 /////////////// Do not edit below this line unless you know what you are doing! ///////////////
 ///////////////////////////////////////////////////////////////////////////////////////////////
-$version = "v0.12";
+$version = "v0.12.1-dev";
 /// Environment ///
 $env = new stdClass();
 $env->action = $settings->defaultaction;
@@ -1025,7 +1025,7 @@ class page_renderer
 
 		<footer>
 			<p>{footer-message}</p>
-			<p>Powered by Pepperminty Wiki v0.12, which was built by <a href='//starbeamrainbowlabs.com/'>Starbeamrainbowlabs</a>. Send bugs to 'bugs at starbeamrainbowlabs dot com' or <a href='//github.com/sbrl/Pepperminty-Wiki' title='Github Issue Tracker'>open an issue</a>.</p>
+			<p>Powered by Pepperminty Wiki v0.12.1-dev, which was built by <a href='//starbeamrainbowlabs.com/'>Starbeamrainbowlabs</a>. Send bugs to 'bugs at starbeamrainbowlabs dot com' or <a href='//github.com/sbrl/Pepperminty-Wiki' title='Github Issue Tracker'>open an issue</a>.</p>
 			<p>Your local friendly administrators are {admins-name-list}.</p>
 			<p>This wiki is managed by <a href='mailto:{admin-details-email}'>{admin-details-name}</a>.</p>
 		</footer>
@@ -1037,7 +1037,7 @@ class page_renderer
 			<p><em>From {sitename}, which is managed by {admin-details-name}.</em></p>
 			<p>{footer-message}</p>
 			<p><em>Timed at {generation-date}</em></p>
-			<p><em>Powered by Pepperminty Wiki v0.12.</em></p>
+			<p><em>Powered by Pepperminty Wiki v0.12.1-dev.</em></p>
 		</footer>";
 
 	// An array of functions that have been registered to process the
@@ -1092,7 +1092,7 @@ class page_renderer
 			"{body}" => $body_template,
 
 			"{sitename}" => $logo_html,
-			"v0.12" => $version,
+			"v0.12.1-dev" => $version,
 			"{favicon-url}" => $settings->favicon,
 			"{header-html}" => self::get_header_html(),
 
@@ -2210,6 +2210,7 @@ register_module([
 				$link = "?page=" . rawurlencode($result["pagename"]);
 				$pagesource = file_get_contents($env->storage_prefix . $result["pagename"] . ".md");
 				$context = search::extract_context($_GET["query"], $pagesource);
+				//echo("Generated search context for " . $result["pagename"] . ": $context\n");
 				$context = search::highlight_context($_GET["query"], $context);
 				/*if(strlen($context) == 0)
 				{
@@ -2340,7 +2341,7 @@ class search
 			
 			self::merge_into_invindex($invindex, ids::getid($pagename), $index);
 		}
-		error_log("Saving inverted index to $paths->searchindex");
+		
 		self::save_invindex($paths->searchindex, $invindex);
 	}
 	
@@ -2557,9 +2558,10 @@ class search
 			}
 		}
 		
+		// Sort the matches by offset
 		usort($matches, function($a, $b) {
 			if($a[1] == $b[1]) return 0;
-			return ($a[1] < $b[1]) ? +1 : -1;
+			return ($a[1] > $b[1]) ? +1 : -1;
 		});
 		
 		$contexts = [];
@@ -2567,8 +2569,7 @@ class search
 		$matches_count = count($matches);
 		while($basepos < $matches_count)
 		{
-			// Store the next match along - all others will be relative to that
-			// one
+			// Store the next match along - all others will be relative to that one
 			$group = [$matches[$basepos]];
 			
 			// Start scanning at the next one along - we always store the first match
@@ -2595,6 +2596,9 @@ class search
 			$context_start = $group[0][1] - $settings->search_characters_context;
 			$context_end = $group[count($group) - 1][1] + $settings->search_characters_context;
 			
+			//echo("Got context. Start: $context_start, End: $context_end\n");
+			//echo("Group:"); var_dump($group);
+			
 			$context = substr($source, $context_start, $context_end - $context_start);
 			
 			// Strip the markdown from the context - it's most likely going to
diff --git a/module_index.json b/module_index.json
index bc15b34..f6eaa19 100755
--- a/module_index.json
+++ b/module_index.json
@@ -68,7 +68,7 @@
         "author": "Starbeamrainbowlabs",
         "description": "Adds proper search functionality to Pepperminty Wiki using an inverted index to provide a full text search engine. If pages don't show up, then you might have hit a stop word. If not, try requesting the `invindex-rebuild` action to rebuild the inverted index from scratch.",
         "id": "feature-search",
-        "lastupdate": 1471597850,
+        "lastupdate": 1471604218,
         "optional": false
     },
     {
diff --git a/modules/feature-search.php b/modules/feature-search.php
index 810cad8..bc7f7e8 100644
--- a/modules/feature-search.php
+++ b/modules/feature-search.php
@@ -137,6 +137,7 @@ register_module([
 				$link = "?page=" . rawurlencode($result["pagename"]);
 				$pagesource = file_get_contents($env->storage_prefix . $result["pagename"] . ".md");
 				$context = search::extract_context($_GET["query"], $pagesource);
+				//echo("Generated search context for " . $result["pagename"] . ": $context\n");
 				$context = search::highlight_context($_GET["query"], $context);
 				/*if(strlen($context) == 0)
 				{
@@ -267,7 +268,7 @@ class search
 			
 			self::merge_into_invindex($invindex, ids::getid($pagename), $index);
 		}
-		error_log("Saving inverted index to $paths->searchindex");
+		
 		self::save_invindex($paths->searchindex, $invindex);
 	}
 	
@@ -484,9 +485,10 @@ class search
 			}
 		}
 		
+		// Sort the matches by offset
 		usort($matches, function($a, $b) {
 			if($a[1] == $b[1]) return 0;
-			return ($a[1] < $b[1]) ? +1 : -1;
+			return ($a[1] > $b[1]) ? +1 : -1;
 		});
 		
 		$contexts = [];
@@ -494,8 +496,7 @@ class search
 		$matches_count = count($matches);
 		while($basepos < $matches_count)
 		{
-			// Store the next match along - all others will be relative to that
-			// one
+			// Store the next match along - all others will be relative to that one
 			$group = [$matches[$basepos]];
 			
 			// Start scanning at the next one along - we always store the first match
@@ -522,6 +523,9 @@ class search
 			$context_start = $group[0][1] - $settings->search_characters_context;
 			$context_end = $group[count($group) - 1][1] + $settings->search_characters_context;
 			
+			//echo("Got context. Start: $context_start, End: $context_end\n");
+			//echo("Group:"); var_dump($group);
+			
 			$context = substr($source, $context_start, $context_end - $context_start);
 			
 			// Strip the markdown from the context - it's most likely going to
diff --git a/version b/version
index fc6f7a7..693ad7d 100644
--- a/version
+++ b/version
@@ -1 +1 @@
-v0.12
+v0.12.1-dev
