Refactoring of queryDB function.