refactor connection and channel handling