code optimisation and refactoring