added check for bad malloc