found a few bugs by inspection.  Refactored.