Added unit tests for caching.  Corrected several misc bugs (i.e. race conditions).