Fix "comparison between signed and unsigned" warnings

Patch from Jonathan Kew.