Big improvement on connection code