Code re-organized for Sync.php