Tidying up some code and adding more documentation