improved malloc error checking