Updated to handle reconnection