Code developed based on the feedback of Roque Pinel