Improve error handling

Also add `LoggedThread` that prints errors to the
console if an error happens during the thread.