improved the connect routine