Added opening and closing of db