Rewrite sync routines.

Complete rewrite of sync routines.
Added setting to simulate a sync, for testing.