Refactor and optimize invalidation