Add several checks to prevent division by zero (triggered by converting black).