Refactoring to split logic from xml generation, added tests