Added check for NaN for tempimg.