implement client factory; fixes to query builder; lots of other fixes and cleanup