Improved error messages and added a test