loop query improved