Add locking when using the db
Small bug fixes