Fix overflow in flatten histogram