Fixed custom query loop bug