Modifications to handle proper connection termincation