First commit of code

This code is cleaned up and simplified (compared to some of the
earlier code), but it is not really new code. It may or may not
work correctly.