Add tests, correct a bug in error handling.