Fixed some bugs for matlab support