use while instead of for loop