diff --git a/build/index.php b/build/index.php
index d1d860e..f8a5ca4 100755
--- a/build/index.php
+++ b/build/index.php
@@ -2209,8 +2209,10 @@ register_module([
 			{
 				$link = "?page=" . rawurlencode($result["pagename"]);
 				$pagesource = file_get_contents($env->storage_prefix . $result["pagename"] . ".md");
+				
+				//echo("Extracting context for result " . $result["pagename"] . ".\n");
 				$context = search::extract_context($_GET["query"], $pagesource);
-				//echo("Generated search context for " . $result["pagename"] . ": $context\n");
+				//echo("'Generated search context for " . $result["pagename"] . ": $context'\n");
 				$context = search::highlight_context($_GET["query"], $context);
 				/*if(strlen($context) == 0)
 				{
@@ -2564,6 +2566,8 @@ class search
 			return ($a[1] > $b[1]) ? +1 : -1;
 		});
 		
+		$sourceLength = strlen($source);
+		
 		$contexts = [];
 		$basepos = 0;
 		$matches_count = count($matches);
@@ -2596,6 +2600,9 @@ class search
 			$context_start = $group[0][1] - $settings->search_characters_context;
 			$context_end = $group[count($group) - 1][1] + $settings->search_characters_context;
 			
+			if($context_start < 0) $context_start = 0;
+			if($context_end > $sourceLength) $context_end = $sourceLength;
+			
 			//echo("Got context. Start: $context_start, End: $context_end\n");
 			//echo("Group:"); var_dump($group);
 			
@@ -2603,7 +2610,10 @@ class search
 			
 			// Strip the markdown from the context - it's most likely going to
 			// be broken anyway.
-			$context = self::strip_markup($context);
+			//$context = self::strip_markup($context);
+			
+			// Escape special characters to protect against attacks
+			$context = htmlentities($context);
 			
 			$contexts[] = $context;
 			
diff --git a/module_index.json b/module_index.json
index f6eaa19..b3a81f0 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": 1471604218,
+        "lastupdate": 1471608025,
         "optional": false
     },
     {
diff --git a/modules/feature-search.php b/modules/feature-search.php
index bc7f7e8..71d7350 100644
--- a/modules/feature-search.php
+++ b/modules/feature-search.php
@@ -136,8 +136,10 @@ register_module([
 			{
 				$link = "?page=" . rawurlencode($result["pagename"]);
 				$pagesource = file_get_contents($env->storage_prefix . $result["pagename"] . ".md");
+				
+				//echo("Extracting context for result " . $result["pagename"] . ".\n");
 				$context = search::extract_context($_GET["query"], $pagesource);
-				//echo("Generated search context for " . $result["pagename"] . ": $context\n");
+				//echo("'Generated search context for " . $result["pagename"] . ": $context'\n");
 				$context = search::highlight_context($_GET["query"], $context);
 				/*if(strlen($context) == 0)
 				{
@@ -491,6 +493,8 @@ class search
 			return ($a[1] > $b[1]) ? +1 : -1;
 		});
 		
+		$sourceLength = strlen($source);
+		
 		$contexts = [];
 		$basepos = 0;
 		$matches_count = count($matches);
@@ -523,6 +527,9 @@ class search
 			$context_start = $group[0][1] - $settings->search_characters_context;
 			$context_end = $group[count($group) - 1][1] + $settings->search_characters_context;
 			
+			if($context_start < 0) $context_start = 0;
+			if($context_end > $sourceLength) $context_end = $sourceLength;
+			
 			//echo("Got context. Start: $context_start, End: $context_end\n");
 			//echo("Group:"); var_dump($group);
 			
@@ -530,7 +537,10 @@ class search
 			
 			// Strip the markdown from the context - it's most likely going to
 			// be broken anyway.
-			$context = self::strip_markup($context);
+			//$context = self::strip_markup($context);
+			
+			// Escape special characters to protect against attacks
+			$context = htmlentities($context);
 			
 			$contexts[] = $context;
 			
