close connection and resources properly