merged upstream changes.

also fixed a bug where cache was sometimes incorrectly not used