added protection against null gradient