Added checks for unexpected termination.