caching results from K for speedup, also some cleaning