Major rewrite of the document generation code and improvements to the parser. Code is simplified and easier to follow. README updated.