Add synchronized flag to sync function. Fixes exception from notifyAll()