More tidying and putting related code together with its explanation.