This is the concept discussed in many books. Not to delay in doing Teshuvah while alive. And the levels are, for example mentioned in Sefer hayashar 13, right after you sin, a long time after continued sinning, when the sin no longer satisfying, when he is sick, when he is in great distress, when he finds a teacher, when he’s old, when he’s dying.
This Teshuvah improves oneself in a spiritual manner. The ideal location for this improvement is before death. Sometimes one is improved with יסורין (physical punishments) as an atonement to get to this improvement. So it says in Yerushalmi, Sanhedrin 10.1, that יסורין can atone.
העובר על הכריתות ומיתות בית דין במזיד התשובה ויום הכיפורים מכפרין מחצה והייסורין מכפרין מחצה ועליו הוא אומר ופקדתי בשבט פשעם וגו'
The goal of this improvement is to get to gan eden (Even Shleimah 10.13). Gahenim is one of three things which must pass over a person before he gets to gan eden (ibid. 10.12). Gehenom is like fire, and it is painful for the נשמה (ibid. 10.33,32).
Teshuvah during life can save one from Gehenom, even if he has already been determined to die (ibid. 10.24). In general, gehenom will remove the spiritual impurities (ibid. 10.31).
So, the point is to change while it’s still in your hands. Otherwise, spiritual ‘punishment’ will accomplish that, for you failed to do so, and can no longer.