Don't access the db twice, eliminate globals

Change-Id: Ie232e5b27082044a5b9c3e91f0b7ce04d38344a3