Fixed infinite timeout handling. Increased code reuse so that there will be fewer copy and paste errors.