code refactor for querying