added sanity check on memory allocation