Fix mex errors and add some codes.