Updated script to work in Matlab