Beginning tidying up of code