moved some code into finally block to close file stream and destroy gui when done writing to file
fixed some mixed tab and spacing problems with previous commit due to IDE