Dealing with integer overflow when calculating the histogram