This code fixes the problem of having multiple connections to the db, and also increases speed by better using the cache.