Fixes to various bugs reported in the wild, including #41