Code reformatting and optimisations