fixed a whole bunch of tiny bugs relating to closing connections