don't do pointer arithmetic with signed numbers
fix c89 declaration order
avoid referencing out-of-bounds memory