Fix for #38, #39 and #59