added query fucntion; refactored