Tweaked to resolve compiler warnings about signed/unsigned comparisions