Added TimeoutThread class to remove code duplication.