Rewritten cache code and fixed a bug that was in it.