Fix for loop from going out of bounds