added error checking for malloc and calloc