ENH: Fix race conditions and provide better debugging.