fix #29 and streamline sync code