For calculating background, added two lines to exclude any pixels
with non-finite values in the calculation