Refactored whole sync process and some minor changes