Some more optimisation and cleanup (mostly suggested by wwood)
One more wwood suggestion that I missed.