added exception handling, improved db locking