Make timeout code more robust