refactor connection closing