Refactoring to improve performance