Rewrote existing code to prevent similar mistakes. R=adonovan CC=golang-dev https://golang.org/cl/7129046