fixed bug and simplified code in sync method