Refactor connection.js