Fixes for #32, #37, #39 and #41