Xtrial and refactoring

Sorry, the changes are not broken down.