changed to using synchronized blocks