small loop iteration fix in custom queries