Fix linkage of mex files and improve MATLAB discovery.