Improved MATLAB Code