Re-implemented timeout function. Needs testing.