Fixed a bug which happened when the program was closed