Added caching and other minor fixes.
Fixed bug with caching.