Better connection handling and cleanups