fixed some bugs. hopefully working 2-phase query system