refactor connection handler