Refactors queryAll.