Refactor, add sync functionality