Fix bug when closing window