3rd time repeat ending with Fine

I would like to get this test piece to play from A to …

B; 1st time bar; repeat B; 2nd time bar.
Then C followed by the DS and then the 3rd time bar and Fine.
However, at the moment, after the 2nd time bar it plays the 3rd time bar D (which I only want as a Fine) and then section C.
After that it works OK.

Am I doing something wrong or do I need to set up 2 Codas?
I tried this other version …

and it works - but since section C is long in the main piece I am doing, it’s a big jump for the player to find.
I would prefer to get the first version working since I think it easier for the players to find their way around.

I think you need an end repeat barline at the end of the second ending as well as the first, no?

With a repeat in the 2nd ending, it just repeats B for the 3rd time, then plays D before carrying on to C ? The DS works OK.

I wanted it to play B twice, then jump to section C, do the DS and then go to the 3rd time bar to play D.
I can see that this is tricky and easy to misunderstand so I suppose the Coda technique is the way to go.
It just seems odd if the Coda is 1 bar long and on the next page!