Added flattening to mat2py, fixed some bugs.