Implemented cache size limiting. Still need a lot of optimization/profiling.