Fixed race condition issue with caching, closes #376.