refaktoring query building, clean up