new version, fixed a race condition