a working version for matlab