Tidied up some code and added comments for new code