correctly close connections