Cleaned up the cache code