Fix for #161

(And some other changes)