diff --git a/Code/Mantid/Framework/Algorithms/src/CreateCalFileByNames.cpp b/Code/Mantid/Framework/Algorithms/src/CreateCalFileByNames.cpp
index 568f90689643237091f54a47b772110da3c26b8f..576e138f382843c13efc755836b44ce4dbcff460 100644
--- a/Code/Mantid/Framework/Algorithms/src/CreateCalFileByNames.cpp
+++ b/Code/Mantid/Framework/Algorithms/src/CreateCalFileByNames.cpp
@@ -211,7 +211,6 @@ namespace Mantid
         int number, udet, select, group;
         double offset;
 
-        instrcalmap::const_iterator it;
         std::string str;
         while(getline(infile,str))
         {
@@ -219,10 +218,12 @@ namespace Mantid
             continue;
           std::istringstream istr(str);
           istr >> number >> udet >> offset >> select >> group;
-          it=instrcalib.find(udet);
-          if (it==instrcalib.end()) // Not found, don't assign a group
-            group=0;
-          group=((*it).second).second; // If found then assign new group
+          instrcalmap::const_iterator it = instrcalib.find(udet);
+          if (it == instrcalib.end()) // Not found, don't assign a group
+            group = 0;
+          else
+            group=((*it).second).second; // If found then assign new group
+          // write out
           writeCalEntry(outfile,number,udet,offset,select,group);
         }
       }
diff --git a/Code/Mantid/Framework/Algorithms/src/CreateDummyCalFile.cpp b/Code/Mantid/Framework/Algorithms/src/CreateDummyCalFile.cpp
index fc259f00766994ab37f443ac67a18cadf21e25dc..48dd491f3983b8b26ac581d2e9d84f640b3374fa 100644
--- a/Code/Mantid/Framework/Algorithms/src/CreateDummyCalFile.cpp
+++ b/Code/Mantid/Framework/Algorithms/src/CreateDummyCalFile.cpp
@@ -264,7 +264,6 @@ namespace Mantid
         int number, udet, select, group;
         double offset;
 
-        instrcalmap::const_iterator it;
         std::string str;
         while(getline(infile,str))
         {
@@ -272,10 +271,12 @@ namespace Mantid
             continue;
           std::istringstream istr(str);
           istr >> number >> udet >> offset >> select >> group;
-          it=instrcalib.find(udet);
-          if (it==instrcalib.end()) // Not found, don't assign a group
-            group=0;
-          group=((*it).second).second; // If found then assign new group
+          instrcalmap::const_iterator it = instrcalib.find(udet);
+          if (it == instrcalib.end()) // Not found, don't assign a group
+            group = 0;
+          else
+            group=((*it).second).second; // If found then assign new group
+          // write to file
           writeCalEntry(outfile,number,udet,offset,select,group);
         }
       }