Tweaks to improve performance, error handling and XML output (instead of XHTML)