Fixes #2215 Double defined $cache on upgrade
Code improvements