Merge fixes for #317, #339, #365 from willum070