tidied up code for xpath and fixed last of bugs - hopefully