adding the code of the replace with the next greatest with comments