All three fixes from #43, #44 and #45 in one PR.