better algo, the goroutine version is slightly slower