Refactor query method.