Improve quoting and unquoting of DTD entities

Clean the code a bit and added support for handling `&amp;`.