refctoring, adding XML context, fixing some bugs