code optimized using gsub