Tided some of the code