warning: comparison of integers of different signs: 'int' and 'unsigned long'

Fixed the warnings, and reduced the scope of loop variables.

If this doesn't fit for any reason, feel free to ignore it.
But I'd like to know why of course. =)