diff --git a/pykat/finesse.py b/pykat/finesse.py
index bd5079859213d0eb4bf62aab03979f0804387240..e34f30a100b1072d47c45962670946eeaab14e15 100644
--- a/pykat/finesse.py
+++ b/pykat/finesse.py
@@ -33,9 +33,8 @@ import pickle
 import pykat
 import warnings
 import re
-import collections
 
-from collections import namedtuple
+from collections import namedtuple, OrderedDict
 
 from pykat.node_network import NodeNetwork
 from pykat.detectors import Detector
@@ -245,7 +244,7 @@ class kat(object):
         
         self.scene = None # scene object for GUI
         self.verbose = True
-        self.__blocks = {} # dictionary of blocks that are used
+        self.__blocks = OrderedDict() # dictionary of blocks that are used
         self.__components = {}  # dictionary of optical components      
         self.__detectors = {}   # dictionary of detectors
         self.__commands = {}    # dictionary of commands
@@ -394,14 +393,14 @@ class kat(object):
                     if values[1] == "FTblock":
                         newTag = values[2]
                         
-                        if self.__currentTag != None and newTag != self.__currentTag: 
+                        if self.__currentTag != None and self.__currentTag != NO_BLOCK: 
                             warnings.warn("found block {0} before block {1} ended".format(newTag, self.__currentTag))    
                             
                         if newTag in self.__blocks:
                             raise pkex.BasePyKatException("Block `{0}` has already been read".format(newTag))
                             
                         self.__blocks[newTag] = Block(newTag) # create new list to store all references to components in block
-                        self.__currentTag = newTag                            
+                        self.__currentTag = newTag
                         
                     if values[1] == "FTend":
                         self.__currentTag = NO_BLOCK
@@ -525,7 +524,7 @@ class kat(object):
                     raise pkex.BasePyKatException("pdtype command `{0}` is incorrect.".format(line))
                     
         self.__currentTag = NO_BLOCK 
-
+        
     def saveScript(self, filename=None):
         """
         Saves the current kat object to a Finesse input file
@@ -805,21 +804,12 @@ class kat(object):
                     if fragment in obj:
                         print "  ** removing line '{0}'".format(obj)
                         objs.remove(obj)
-        
+
+                    
     def generateKatScript(self) :
         """ Generates the kat file which can then be run """
-        
-        out = []    
-        import datetime
-        strtoday = datetime.datetime.now()
-        out.append(strtoday.strftime("%% Generated by PyKat %d.%m.%Y %H:%M:%S\n") )
-        
-        for key in self.__blocks:
-            objs = self.__blocks[key].contents
 
-            if key != NO_BLOCK:
-                out.append("%%% FTblock " + key + "\n")
-            
+        def writeBlock():
             for obj in objs:
                 if isinstance(obj, str):
                     out.append(obj + '\n')
@@ -834,9 +824,33 @@ class kat(object):
                         else:
                             out.append(txt + "\n")
 
+        
+        out = []    
+        import datetime
+        strtoday = datetime.datetime.now()
+        out.append(strtoday.strftime("%% Generated by PyKat %d.%m.%Y %H:%M:%S\n") )
+
+        # write the FTblocks
+        for key in self.__blocks:
+            objs = self.__blocks[key].contents
+
             if key != NO_BLOCK:
-                out.append("%%% FTend " + key + "\n")
-            
+                if np.size(objs)>0:
+                    out.append("\n")
+                    out.append("%%% FTblock " + key + "\n")
+                    writeBlock()
+                    out.append("%%% FTend " + key + "\n")
+
+        # write the NO_BLOCK blocks
+        for key in self.__blocks:
+            objs = self.__blocks[key].contents
+
+
+            if key == NO_BLOCK:
+                if np.size(objs)>0:
+                    out.append("\n")
+                    writeBlock()
+                
         # now loop through all the nodes and get any gauss commands
         for key in self.nodes.getNodes():
             txt = self.nodes.getNodes()[key].getFinesseText()
@@ -856,7 +870,6 @@ class kat(object):
                 for t in txt: out.append(t+ "\n")
             else:
                 out.append(txt + "\n")
-                            
 
         if self.vacuum != None: