Added comments / tidied code where appropriate

Added comments / tidied code where appropriate.