Code refactoring (using xquery, extracting methods, removing repetable code)