refactor

Refactored for use with matlab code