close connections properly