Factor out timeout code