Changed code for older MATLAB versions