Added comments and tidied up code.