add more verbose error messages and add more memory checks