refactor connection, add tests