fix to close connections correctly