Modified to handle timeouts gracefully.