restore select uses of gsub! and minor optimizations