Tidied up some code and started commenting