fixing Query method and adding locks