Fixed a subtle bug; plus other minor changes