sync function draft done, lots of refactoring! testing needed