Fix overflow in scaling algorithm.