added two more synchronized blocks to avoid unspecified behaviour iterating over synchronizedSet