annotated the code and tidied and replaced unused code