refactoring sync function