Improve error messages and tests