fix too long query
bug fix for loop