Advice on how to adjust this to use a for loop