Consider refactoring and splitting up sync.py
As noted in !7 (merged) the main sync.py script has become quite long and unwieldy. Consider refactoring it, splitting it up into smaller pieces. The main block comments indicate the various stages of the sync job, and might mark sensible places to split it up.