Refactor query_values method