Refactor of connection.rb to make code DRYer.