adapted for matlab