Working version.  Needs comments.  But the code works.