refactoring code. tag.buffer seems to be unused
13 tests still failing but some issues with escaping are fixed