Fix some general bugs related to closing the program