fix race condition, and replace mutex with chan to communicate among goroutines.