Race conditions and caching variables