Closes connections properly