Fix a few bugs.  Tip of the hat to Ashok Gelal <ashokgelal@gmail.com>