Fixing issue with looping through results