Adding tests, refactoring some sync code.