Final optimisation and refactoring