Several improvements thanks to rogpeppe from #go-nuts