tweaking close connection logic