adding comments and tidying up code