A few optimisations
Tidy up of code