Added code to handle timeouts properly