Refactored cache compaction