diff --git a/FileIO/Legacy/OGSIOVer4.cpp b/FileIO/Legacy/OGSIOVer4.cpp
index 39fafc11b7d840da4bfb4156778656dee9c48af8..5eff3bf3d6566906cc2136483a60b3a4c920a7c9 100644
--- a/FileIO/Legacy/OGSIOVer4.cpp
+++ b/FileIO/Legacy/OGSIOVer4.cpp
@@ -565,6 +565,48 @@ bool readGLIFileV4(const std::string& fname,
 		return false;
 }
 
+std::size_t writeTINSurfaces(std::ofstream &os,
+	GeoLib::SurfaceVec const* sfcs_vec,
+	std::size_t sfc_count,
+	std::string const& path)
+{
+	const std::vector<GeoLib::Surface*>* sfcs (sfcs_vec->getVector());
+	for (std::size_t k(0); k < sfcs->size(); k++)
+	{
+		std::string sfc_name;
+		if (! sfcs_vec->getNameOfElementByID (sfc_count, sfc_name)) {
+			sfc_name = std::to_string (sfc_count);
+		}
+		std::string const sfc_file_name(sfc_name +".tin");
+		std::string const sfc_file_path(path + sfc_file_name);
+
+		// create tin file
+		std::ofstream tin_os (sfc_file_path.c_str());
+		if (! tin_os.good()) {
+			ERR("Could not write TIN data to file \"%s\".",
+				sfc_file_path.c_str());
+			continue;
+		}
+		GeoLib::Surface const& sfc (*(*sfcs)[k]);
+		const std::size_t n_tris (sfc.getNTriangles());
+		for (std::size_t l(0); l < n_tris; l++) {
+			GeoLib::Triangle const& tri (*(sfc[l]));
+			tin_os << l << " " << *(tri.getPoint(0)) << " " <<
+				*(tri.getPoint(1)) << " " << *(tri.getPoint(2)) << "\n";
+		}
+		tin_os.close();
+		sfc_count++;
+
+		// create entry/link in gli file
+		os << "#SURFACE\n";
+		os << "  $NAME\n";
+		os << "    " << sfc_name << "\n";
+		os << "  $TIN\n";
+		os << "    " << sfc_file_name << "\n";
+	}
+	return sfc_count;
+}
+
 void writeGLIFileV4 (const std::string& fname,
                      const std::string& geo_name,
                      const GeoLib::GEOObjects& geo)
@@ -617,40 +659,9 @@ void writeGLIFileV4 (const std::string& fname,
 	}
 
 	// writing surfaces as TIN files
-	const std::string path = BaseLib::extractPath(fname);
-	std::size_t sfcs_cnt (0);
-	const GeoLib::SurfaceVec* sfcs_vec (geo.getSurfaceVecObj (geo_name]));
-	if (sfcs_vec) {
-		const std::vector<GeoLib::Surface*>* sfcs (sfcs_vec->getVector());
-		for (std::size_t k(0); k < sfcs->size(); k++)
-		{
-			os << "#SURFACE" << "\n";
-			std::string sfc_name;
-			if (sfcs_vec->getNameOfElementByID (sfcs_cnt, sfc_name)) {
-				os << "\t$NAME " << "\n" << "\t\t" << sfc_name << "\n";
-			} else {
-				os << "\t$NAME " << "\n" << "\t\t" << sfcs_cnt << "\n";
-				sfc_name = std::to_string (sfcs_cnt);
-			}
-			sfc_name += ".tin";
-			os << "\t$TIN" << "\n";
-			os << "\t\t" << sfc_name << "\n";
-			// create tin file
-			sfc_name = path + sfc_name;
-			std::ofstream tin_os (sfc_name.c_str());
-			GeoLib::Surface const& sfc (*(*sfcs)[k]);
-			const std::size_t n_tris (sfc.getNTriangles());
-			for (std::size_t l(0); l < n_tris; l++) {
-				GeoLib::Triangle const& tri (*(sfc[l]));
-			tin_os << l << " " << *(tri.getPoint(0)) << " " <<
-			*(tri.getPoint(1)) << " " << *(tri.getPoint(2)) <<
-			"\n";
-			}
-			tin_os.close();
-
-			sfcs_cnt++;
-		}
-	}
+	const GeoLib::SurfaceVec* sfcs_vec (geo.getSurfaceVecObj (geo_name));
+	if (sfcs_vec) 
+		writeTINSurfaces(os, sfcs_vec, 0, BaseLib::extractPath(fname));
 
 	os << "#STOP" << "\n";
 	os.close ();
@@ -742,37 +753,8 @@ void writeAllDataToGLIFileV4 (const std::string& fname, const GeoLib::GEOObjects
 	for (std::size_t j(0); j < geo_names.size(); j++)
 	{
 		const GeoLib::SurfaceVec* sfcs_vec (geo.getSurfaceVecObj (geo_names[j]));
-			if (sfcs_vec) {
-				const std::vector<GeoLib::Surface*>* sfcs (sfcs_vec->getVector());
-				for (std::size_t k(0); k < sfcs->size(); k++)
-				{
-					os << "#SURFACE" << "\n";
-					std::string sfc_name;
-					if (sfcs_vec->getNameOfElementByID (sfcs_cnt, sfc_name)) {
-						os << "\t$NAME " << "\n" << "\t\t" << sfc_name << "\n";
-					} else {
-						os << "\t$NAME " << "\n" << "\t\t" << sfcs_cnt << "\n";
-					sfc_name = std::to_string (sfcs_cnt);
-				}
-				sfc_name += ".tin";
-				os << "\t$TIN" << "\n";
-				os << "\t\t" << sfc_name << "\n";
-				// create tin file
-				sfc_name = path + sfc_name;
-				std::ofstream tin_os (sfc_name.c_str());
-				GeoLib::Surface const& sfc (*(*sfcs)[k]);
-				const std::size_t n_tris (sfc.getNTriangles());
-					for (std::size_t l(0); l < n_tris; l++) {
-						GeoLib::Triangle const& tri (*(sfc[l]));
-					tin_os << l << " " << *(tri.getPoint(0)) << " " <<
-					*(tri.getPoint(1)) << " " << *(tri.getPoint(2)) <<
-					"\n";
-				}
-				tin_os.close();
-
-				sfcs_cnt++;
-			}
-		}
+		if (sfcs_vec)
+			sfcs_cnt += writeTINSurfaces(os, sfcs_vec, sfcs_cnt, path);
 	}
 
 	os << "#STOP" << "\n";