bug fixing and streamlining queries