implemented better closing of db connections