fixed path function, moved QUERY initialization in the for loop