Better fix for nested tags

Also move some code to gsub! method to get rid of code duplication