diff --git a/res/search.css b/res/search.css
index ce60df48ec8d219e3fe05d449a6c02a4750a0d02..41cbbc0068e95ffa80e398a31363ffa296c70b69 100644
--- a/res/search.css
+++ b/res/search.css
@@ -91,7 +91,7 @@ nav {
     right: 0;
     top: 0;
     height: 56px;
-    z-index: 1;
+    z-index: 2;
 }
 
 nav .wrapper {
@@ -159,7 +159,7 @@ nav input[type=text] {
 }
 
 #no_more img {
-    margin: 8rem auto 0px;
+    margin: 8rem auto 0;
     display: block;
 }
 
@@ -174,7 +174,7 @@ nav input[type=text] {
 
 inline-button {
     display: block;
-    background: rgb(255, 255, 255) none repeat scroll 0% 0%;
+    background: rgb(255, 255, 255) none repeat scroll 0 0;
     font-size: 16px;
     height: 48px;
     line-height: 48px;
diff --git a/res/search.js b/res/search.js
index a932eb3330161068514f1a2faf92fc29dd579aad..4cebbe4b5b1f71f1ff61e3144281721af50d6494 100644
--- a/res/search.js
+++ b/res/search.js
@@ -20,12 +20,8 @@ var get_history = function () {
 
 var wrap_click_handler = function (fun) {
     return function (event) {
-        var sel = getSelection().toString();
-        if (sel) {
-            event.stopPropagation();
-        } else if (fun) {
-            fun(event);
-        }
+        event.stopPropagation();
+        fun(event);
     }
 };
 
@@ -52,14 +48,6 @@ $("#q").keypress(function (e) {
     }
 });
 
-$("body").click(function (e) {
-    open[open.length-1](e);
-});
-
-$("nav").click(function (e) {
-    e.stopPropagation();
-});
-
 $("#q").focus(function (){
     $("#autocomplete").addClass("active");
     $("#results").addClass("hidden");
@@ -175,7 +163,9 @@ var attach_buffer = function (elem) {
         e.stopPropagation();
     }));
     buffers[id].contexts.forEach(function (context) {
-        attach_context(elem.find("#context" + context.id));
+        var context = elem.find("#context" + context.id);
+        if (context.length)
+            attach_context(context);
     })
 };
 
@@ -239,6 +229,10 @@ var attach_context = function (elem) {
     elem.unbind();
     var id = elem.data("contextid");
     var bufferid = elem.data("bufferid");
+    if (buffers[bufferid] === undefined) {
+        console.log("Undefined buffer: " + bufferid);
+    }
+
     elem.click(wrap_click_handler(function (e) {e.stopPropagation(); }));
     $("#message"+buffers[bufferid].contexts[id].original.messageid).click(wrap_click_handler(make_toggle_context(bufferid, id)));
     elem.find(".load_before").click(wrap_click_handler(function (e) {
@@ -393,6 +387,7 @@ var make_toggle_buffer = function (id) {
 
 var select_buffer = function (id) {
     deselect_buffers(id);
+    open.push(make_toggle_buffer(id));
     buffers[id].selected = true;
     update_buffer(id);
 };
@@ -411,9 +406,10 @@ var make_toggle_context = function (buffer, id) {
             open.pop();
         } else {
             unselect_contexts(buffer);
-            if (!buffers[buffer].selected)
+            if (!buffers[buffer].selected) {
                 open.push(make_toggle_buffer(buffer));
-            buffers[buffer].selected = true;
+                buffers[buffer].selected = true;
+            }
             context.selected = true;
             open.push(make_toggle_context(buffer, id));
             if (context.before.length === 0) earlier(buffer, id, 5);
@@ -462,4 +458,15 @@ var sort_messages = function (arr) {
     });
 };
 
-update_history();
\ No newline at end of file
+update_history();
+
+
+
+$("body").click(function (e) {
+    if (open.length)
+        open[open.length - 1](e);
+});
+
+$("nav").click(function (e) {
+    e.stopPropagation();
+});
\ No newline at end of file
diff --git a/templates/search.phtml b/templates/search.phtml
index bbdad90302800c22b65ef6ea65530767656ab172..fe77ea06ac491a4120d5c5602de4d992455fad7f 100644
--- a/templates/search.phtml
+++ b/templates/search.phtml
@@ -29,6 +29,7 @@
 </section>
 
 <div id="modal-background"></div>
+<div id="bg"></div>
 
 <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
 <script src="res/search.js"></script>