General tidying up of the code