Re-applying bugfixes.

@nikolay @tdlm here are the fixes that we've already applied to batcache in production