Fixed db close issue, in a cleaner way