More code optimisations and cleanup