Fix a number of issues in the first version.  This works but could do
with some style improvements.