Some refactoring by Xav and Sergi