Improve handling of closed connections (#148)