better handling of connection termination