Various fixes

I'm not completely sure about some of these, although the test pass: review & comments welcome.