Large scale refactoring and addition of cache