Fix some bugs uncovered by go run -race tool