Made changes to make it run in MATLAB