Fixed and optimized #7 in Go