Improved connection routine