Added NULL check on malloc