Cloud sync now works. Plus some refactoring.