Clean up connections

The code seems much less racy now.