refactor code using ModelQuery