diff --git a/DynamicModel.cc b/DynamicModel.cc
index b7eb27d853e7b8f424d75221d76ab59bf71d2eac..339cbe9765a2bfbbc032bbcc0774d59d60827cae 100644
--- a/DynamicModel.cc
+++ b/DynamicModel.cc
@@ -4289,24 +4289,29 @@ DynamicModel::isChecksumMatching(const string &basename) const
       result.process_bytes(private_buffer,strlen(private_buffer));
     }
 
+  bool basename_dir_exists = false;
+#ifdef _WIN32
+  int r = mkdir(basename.c_str());
+#else
+  int r = mkdir(basename.c_str(), 0777);
+#endif
+  if (r < 0)
+    if (errno != EEXIST)
+      {
+	perror("ERROR");
+	exit(EXIT_FAILURE);
+      }
+    else
+      basename_dir_exists = true;
+
   // check whether basename directory exist. If not, create it.
   // If it does, read old checksum if it exist
   fstream checksum_file;
   string filename = basename + "/checksum";
   unsigned int old_checksum = 0;
-  struct stat sd;
-  if (stat(basename.c_str(), &sd) != 0)
-    {
-      const int dir_err = mkdir(basename.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
-      if (dir_err == -1)
-	{
-	  cerr << "ERROR: can't create directory " << basename << endl;
-	  exit(EXIT_FAILURE);
-	}
-    }
-  else
+  // read old checksum if it exists
+  if (basename_dir_exists)
     {
-      // read old checksum if it exists
       checksum_file.open(filename.c_str(), ios::in | ios::binary);
       if (checksum_file.is_open())
 	{