Fixed several bugs, optimized use of XPath queries.