Tidy up and make tests more robust.