Fixed some exceptions when window is closing