Some refactoring/optimisations