Correct calculation of end of a programme