Fix a bug when closing