Possible race condition and cleanup