Refactor URL parsing, organize connection-related code