I can't believe this actually -works-. Fix to make database code thred-safe.