Added comments.
Tidied up the informal scripts into functions.