Improve sync logic, refactor, update test script