diff --git a/include/opendht/securedht.h b/include/opendht/securedht.h
index 287dca53fe96a410c8f9b5aefa6f3ab2ee2bbacd..f5382708c0302ae4c3d7df9b263acb2cd6a6eca0 100644
--- a/include/opendht/securedht.h
+++ b/include/opendht/securedht.h
@@ -346,8 +346,6 @@ private:
     std::map<InfoHash, Sp<crypto::Certificate>> nodesCertificates_ {};
     std::map<InfoHash, Sp<const crypto::PublicKey>> nodesPubKeys_ {};
 
-    std::uniform_int_distribution<Value::Id> rand_id {};
-
     std::atomic_bool forward_all_ {false};
 };
 
diff --git a/src/securedht.cpp b/src/securedht.cpp
index 3b67abdb7e9d47dce3cf6a4c5ccac3784485457f..2fa3254f033e20ebf88a22fec57690488d5be6f6 100644
--- a/src/securedht.cpp
+++ b/src/securedht.cpp
@@ -213,11 +213,13 @@ SecureDht::findPublicKey(const InfoHash& node, std::function<void(const Sp<const
     findCertificate(node, [=](const Sp<crypto::Certificate> crt) {
         if (crt && *crt) {
             auto pk = std::make_shared<crypto::PublicKey>(crt->getPublicKey());
-            nodesPubKeys_[pk->getId()] = pk;
-            if (cb) cb(pk);
-        } else {
-            if (cb) cb(nullptr);
+            if (*pk) {
+                nodesPubKeys_[pk->getId()] = pk;
+                if (cb) cb(pk);
+                return;
+            }
         }
+        if (cb) cb(nullptr);
     });
 }
 
@@ -236,7 +238,8 @@ SecureDht::checkValue(const Sp<Value>& v)
         try {
             Value decrypted_val (decrypt(*v));
             if (decrypted_val.recipient == getId()) {
-                nodesPubKeys_[decrypted_val.owner->getId()] = decrypted_val.owner;
+                if (decrypted_val.owner)
+                    nodesPubKeys_[decrypted_val.owner->getId()] = decrypted_val.owner;
                 return std::make_shared<Value>(std::move(decrypted_val));
             }
             // Ignore values belonging to other people
@@ -317,6 +320,7 @@ SecureDht::putSigned(const InfoHash& hash, Sp<Value> val, DoneCallback callback,
 {
     if (val->id == Value::INVALID_ID) {
         crypto::random_device rdev;
+        std::uniform_int_distribution<Value::Id> rand_id;
         val->id = rand_id(rdev);
     }