CHANGED the way DB is handled to avoid memory leaks and to avoid opening connection multiple times