Mass code optimisations and code formatting changes