Changed types and added casts so that build is clean with gcc -Wsign-compare.