modified for matlab use