Added flexible #masks for MM to the big functions
Simple merge, only bottom commented lines etc. changed.