Simplify some conditions and make some variables unsigned.

This is mainly done to avoid overflows of signed integers, which
is not defined.