some improvements but race conditions still possible