Changed max value
Fixed loop index subtraction