First stage of diagtool step 2 and some code tidying up