Refactored DB synchronization logic