Used a reverse iteration to improve the performance of the code