fixed a potential overflow in sum