Clean up correctly when a window is closed.