more untested code...
sync not functioning, but refactored to be cleaner