Modification according to MatLab code