Added array length comparison, refactored array index comparison to stop at minimum array length.