Rework race condition fix

travis and other comments have been taken into account.