Changed for loop condition to be more generalized rather than in fixed range of integers