General tidying up, plus adding logging