map cleanup and race condition prevention