Finished refactoring query.lua