refactoring and optimisations