Modified for loop i bug