Added lots of commenting and fixed up the code