Added some synchronization code to fix bugs

Other minor fixes too