Fix signed/unsigned comparisons

Fixed all signed/unsigned comparison warnings. Please review for correctness and merge.