Re-organised code into proper functions.

Will be easier to customise the output.

Signed-off-by: Duncan <duncan@oeith.co.uk>
Code and output re-organised.

(Looking a little less embarrasing.)

Signed-off-by: Duncan <duncan@oeith.co.uk>