minor improvements
fixed race condition