More rewrite. This version builds and is thought to be complete.