handle more error cases.  Needs a bigger cleanup.
Make Database interactions threadsafe.
fix count bug