added more comments and tidied up code