diff --git a/modules/io/src/mol/pdb_writer.cc b/modules/io/src/mol/pdb_writer.cc
index ffbd145fab23b7c5734c1126023858905d02c1df..45115e9a7afc015e228055629504a65d7c4e44c4 100644
--- a/modules/io/src/mol/pdb_writer.cc
+++ b/modules/io/src/mol/pdb_writer.cc
@@ -233,17 +233,21 @@ public:
   
   virtual bool VisitChain(const mol::ChainHandle& chain)
   {
-    if (peptide_) {
-      this->WriteTer(prev_);
+    if (chain.GetResidueCount()!=0) {
+      if (peptide_) {
+        this->WriteTer(prev_);
+      }
     }
     return true;
   }
+
   virtual void OnExit()
   {
     if (peptide_) {
       this->WriteTer(prev_);
     }
   }
+
   void WriteTer(mol::ResidueHandle res)
   {
     counter_++;
diff --git a/modules/io/tests/test_io_pdb.cc b/modules/io/tests/test_io_pdb.cc
index 982bd2f463e82633b5b1e411b168503f5e481eb6..4c69702bfec44c01384abb966618d1bf3ac6af49 100644
--- a/modules/io/tests/test_io_pdb.cc
+++ b/modules/io/tests/test_io_pdb.cc
@@ -373,6 +373,48 @@ BOOST_AUTO_TEST_CASE(write_ter3)
                             "testfiles/pdb/ter3-out.pdb"));
 }
 
+BOOST_AUTO_TEST_CASE(write_ter4)
+{
+  {
+    PDBWriter writer(String("testfiles/pdb/ter_emptychain-out.pdb"), IOProfile());
+
+    mol::EntityHandle ent=mol::CreateEntity();
+    mol::XCSEditor edi=ent.EditXCS();
+    mol::ChainHandle ch=edi.InsertChain("A");
+    mol::ResidueHandle r=edi.AppendResidue(ch, "ALA");
+    mol::AtomHandle a=edi.InsertAtom(r, "N", geom::Vec3(32.0, -128.0, -2.5));
+    mol::ResidueHandle r2=edi.AppendResidue(ch, "GLY");
+    mol::AtomHandle a2=edi.InsertAtom(r2, "N", geom::Vec3(35.0, -99.0, -10.5));
+    mol::ChainHandle ch2=edi.InsertChain("B");
+    conop::Conopology& conop_inst=conop::Conopology::Instance();
+    conop_inst.ConnectAll(conop_inst.GetBuilder(), ent);
+    writer.Write(ent);
+  }
+  BOOST_CHECK(compare_files("testfiles/pdb/ter_emptychain.pdb",
+                            "testfiles/pdb/ter_emptychain-out.pdb"));
+}
+
+BOOST_AUTO_TEST_CASE(write_ter5)
+{
+  {
+    PDBWriter writer(String("testfiles/pdb/ter_view-emptychain-out.pdb"), IOProfile());
+
+    mol::EntityHandle ent=mol::CreateEntity();
+    mol::XCSEditor edi=ent.EditXCS();
+    mol::ChainHandle ch=edi.InsertChain("A");
+    mol::ResidueHandle r=edi.AppendResidue(ch, "ALA");
+    mol::AtomHandle a=edi.InsertAtom(r, "N", geom::Vec3(32.0, -128.0, -2.5));
+    mol::ResidueHandle r2=edi.AppendResidue(ch, "GLY");
+    mol::AtomHandle a2=edi.InsertAtom(r2, "N", geom::Vec3(35.0, -99.0, -10.5));
+    mol::ChainHandle ch2=edi.InsertChain("B");
+    conop::Conopology& conop_inst=conop::Conopology::Instance();
+    conop_inst.ConnectAll(conop_inst.GetBuilder(), ent);
+    writer.Write(ent.Select(""));
+  }
+  BOOST_CHECK(compare_files("testfiles/pdb/ter_view-emptychain.pdb",
+                            "testfiles/pdb/ter_view-emptychain-out.pdb"));
+}
+
 BOOST_AUTO_TEST_CASE(write_conect)
 {
   // this scope is required to force the writer stream to be closed before
diff --git a/modules/io/tests/testfiles/pdb/ter_emptychain.pdb b/modules/io/tests/testfiles/pdb/ter_emptychain.pdb
new file mode 100644
index 0000000000000000000000000000000000000000..129e5225f21b0d15ddb37a9115c35b4d4a5960a6
--- /dev/null
+++ b/modules/io/tests/testfiles/pdb/ter_emptychain.pdb
@@ -0,0 +1,4 @@
+ATOM      1  N   ALA A   1      32.000-128.000  -2.500  1.00  0.00           N  
+ATOM      2  N   GLY A   2      35.000 -99.000 -10.500  1.00  0.00           N  
+TER       3      GLY A   2                                                      
+END   
diff --git a/modules/io/tests/testfiles/pdb/ter_view-emptychain.pdb b/modules/io/tests/testfiles/pdb/ter_view-emptychain.pdb
new file mode 100644
index 0000000000000000000000000000000000000000..129e5225f21b0d15ddb37a9115c35b4d4a5960a6
--- /dev/null
+++ b/modules/io/tests/testfiles/pdb/ter_view-emptychain.pdb
@@ -0,0 +1,4 @@
+ATOM      1  N   ALA A   1      32.000-128.000  -2.500  1.00  0.00           N  
+ATOM      2  N   GLY A   2      35.000 -99.000 -10.500  1.00  0.00           N  
+TER       3      GLY A   2                                                      
+END