Minor code change to prevent bug from happening

One loop has been moved into the right location..