improved error reporting and added selftest