Rewrite this as much as possible.  No functional changes.

(cherry picked from commit 83af8bd01b43f4fed3ce01d33bea3fcb8ac95b2e)
Signed-off-by: Bruce A. Mah <bmah@es.net>