Various cleanups and fixes

This branch makes various cleanups and fixes to the library, in separate commits.