diff --git a/src/search.h b/src/search.h
index ad7fc487f07a5ac040676183466114a2f98a770e..99b6e3435e1830fba9dca7f77d9dbed491520d05 100644
--- a/src/search.h
+++ b/src/search.h
@@ -854,12 +854,17 @@ Dht::Search::insertNode(const Sp<Node>& snode, time_point now, const Blob& token
             expired = false;
         }
 
-        while (nodes.size() - bad >  SEARCH_NODES) {
+        while (nodes.size() - bad > SEARCH_NODES) {
+            bool removingNode = nodes.back()->node == snode;
             if (not expired and nodes.back()->isBad())
                 bad--;
             nodes.pop_back();
+            if (removingNode)
+                return false;
         }
     }
+    if (n == nodes.end() or not *n)
+        return false;
     if (not token.empty()) {
         (*n)->candidate = false;
         (*n)->last_get_reply = now;