factor out common sync code