B various fixes

Contains six bug fixes: #141, #178, #266, #271, #274, #278. See the description of the bugs for the details on what was going wrong.