Made database scanning thread-safe