tidied up and commented a bit