refactored sync logic