Race condition fixes

based on code review