extracted query() method