Add a timeout mechanism

Issue #337