Fix leak in the tests Also introduce the slective ref trimming promised but also missed in the previous commit.