Refactored to improve performance