Added proper termination of connection