cleanup code
replace while by for loop where appropriate
correct sign/unsign mismatch