Add functionality to catch the window close event too