progress toward #230. All re-written, but bugs need fixed