Fix integer overflow (thanks Hauke Mertens)