Refactored for efficiency