improved error checking, reporting and testcases