Fixed bug when closing connection