Massive tidying of code