Implement timeout, and add several tests.