Improve formatting to match MATLAB best practice
Remove duplicate code