Rebuilt the timeout function

Now uses sleep rather than calander. This reduces CPU and memory usage.