added caching logic, refactored to 2 ops