Fix issues #154 and #229, and a few other improvements