fix query loop