improved error messages and fixed hat test