Refactored for query efficiency