diff --git a/MeshLib/IO/VtkIO/VtuInterface-impl.h b/MeshLib/IO/VtkIO/VtuInterface-impl.h
index 34ed395d7c1902730c64be2b07b891188c313fa4..fc140f89e60dba7eaf12456f99be58dbbabc3607 100644
--- a/MeshLib/IO/VtkIO/VtuInterface-impl.h
+++ b/MeshLib/IO/VtkIO/VtuInterface-impl.h
@@ -24,13 +24,20 @@
 #include "MeshLib/Vtk/VtkMappedMeshSource.h"
 #include "VtuInterface.h"
 
+#ifdef USE_PETSC
+#include <mpi.h>
+#endif
+
+class vtkXMLPUnstructuredGridWriter;
+
 namespace MeshLib
 {
 namespace IO
 {
 template <typename UnstructuredGridWriter>
 bool VtuInterface::writeVTU(std::string const& file_name,
-                            const int num_partitions, const int rank)
+                            [[maybe_unused]] const int num_partitions,
+                            [[maybe_unused]] const int rank)
 {
     if (!_mesh)
     {
@@ -71,16 +78,15 @@ bool VtuInterface::writeVTU(std::string const& file_name,
     }
 
     vtuWriter->SetFileName(file_name.c_str());
-#ifdef USE_PETSC
-    vtuWriter->SetGhostLevel(1);
-    vtuWriter->SetNumberOfPieces(num_partitions);
-    vtuWriter->SetStartPiece(rank);
-    vtuWriter->SetEndPiece(rank);
-#else
-    // avoid unused parameter warnings.
-    (void)num_partitions;
-    (void)rank;
-#endif
+
+    if constexpr (std::is_same_v<UnstructuredGridWriter,
+                                 vtkXMLPUnstructuredGridWriter>)
+    {
+        vtuWriter->SetGhostLevel(1);
+        vtuWriter->SetNumberOfPieces(num_partitions);
+        vtuWriter->SetStartPiece(rank);
+        vtuWriter->SetEndPiece(rank);
+    }
 
 #ifdef VTK_USE_64BIT_IDS
     vtuWriter->SetHeaderTypeToUInt64();
diff --git a/MeshLib/IO/VtkIO/VtuInterface.cpp b/MeshLib/IO/VtkIO/VtuInterface.cpp
index 032a0f12e6e925e1f373026cde3f416cad267733..093cfe5f199c6cf2934da71c69c791d9ac1b7077 100644
--- a/MeshLib/IO/VtkIO/VtuInterface.cpp
+++ b/MeshLib/IO/VtkIO/VtuInterface.cpp
@@ -138,17 +138,21 @@ std::string getVtuFileNameForPetscOutputWithoutExtension(
 bool VtuInterface::writeToFile(std::filesystem::path const& file_path)
 {
 #ifdef USE_PETSC
-    auto const vtu_file_name =
-        getVtuFileNameForPetscOutputWithoutExtension(file_path.string());
-    int rank;
-    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
-    int mpi_size;
-    MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
-    return writeVTU<vtkXMLPUnstructuredGridWriter>(vtu_file_name + ".pvtu",
-                                                   mpi_size, rank);
-#else
-    return writeVTU<vtkXMLUnstructuredGridWriter>(file_path.string());
+    int mpi_init;
+    MPI_Initialized(&mpi_init);
+    if (mpi_init == 1)
+    {
+        auto const vtu_file_name =
+            getVtuFileNameForPetscOutputWithoutExtension(file_path.string());
+        int rank;
+        MPI_Comm_rank(MPI_COMM_WORLD, &rank);
+        int mpi_size;
+        MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
+        return writeVTU<vtkXMLPUnstructuredGridWriter>(vtu_file_name + ".pvtu",
+                                                       mpi_size, rank);
+    }
 #endif
+    return writeVTU<vtkXMLUnstructuredGridWriter>(file_path.string());
 }
 
 int writeVtu(MeshLib::Mesh const& mesh, std::string const& file_name,
diff --git a/MeshLib/IO/readMeshFromFile.cpp b/MeshLib/IO/readMeshFromFile.cpp
index 8e4af64a86d05ccd3c3cf94386e89b4cb6d1b826..c81780ea3d33702d6e01293069612c478b42aae9 100644
--- a/MeshLib/IO/readMeshFromFile.cpp
+++ b/MeshLib/IO/readMeshFromFile.cpp
@@ -68,27 +68,28 @@ namespace IO
 MeshLib::Mesh* readMeshFromFile(const std::string& file_name)
 {
 #ifdef USE_PETSC
-    int world_size;
-    MPI_Comm_size(MPI_COMM_WORLD, &world_size);
-    if (world_size > 1)
+    int mpi_init;
+    MPI_Initialized(&mpi_init);
+    if (mpi_init == 1)
     {
-        MeshLib::IO::NodePartitionedMeshReader read_pmesh(MPI_COMM_WORLD);
-        const std::string file_name_base =
-            BaseLib::dropFileExtension(file_name);
-        return read_pmesh.read(file_name_base);
+        int world_size;
+        MPI_Comm_size(MPI_COMM_WORLD, &world_size);
+        if (world_size > 1)
+        {
+            MeshLib::IO::NodePartitionedMeshReader read_pmesh(MPI_COMM_WORLD);
+            const std::string file_name_base =
+                BaseLib::dropFileExtension(file_name);
+            return read_pmesh.read(file_name_base);
+        }
+        if (world_size == 1)
+        {
+            std::unique_ptr<Mesh> mesh{readMeshFromFileSerial(file_name)};
+            return new MeshLib::NodePartitionedMesh(*mesh);
+        }
+        return nullptr;
     }
-    else if (world_size == 1)
-    {
-        MeshLib::Mesh* mesh = readMeshFromFileSerial(file_name);
-        MeshLib::NodePartitionedMesh* part_mesh =
-            new MeshLib::NodePartitionedMesh(*mesh);
-        delete mesh;
-        return part_mesh;
-    }
-    return nullptr;
-#else
-    return readMeshFromFileSerial(file_name);
 #endif
+    return readMeshFromFileSerial(file_name);
 }
 
 }  // end namespace IO