Refactored and optimized performance.