Refactor query func

:dancers: