Refactored sync functionality to be more readable