Refactor connect()