Added proper connection closing upon program exit