rewrite of sync logic, seems to be working, lots of cleanup to do