trying to improve the logic for handling bad connections