Bunch of improvements/fixes to cache merging