Big refactor: to functions
Fix a bug in xpath tests