Refactoring optimisation of code