Backported various changes from DivanDesign MM0.6.x