Thread safe code

Added some thread-safe code to Database.cs