added some safegards in the timeout handler