Gros refactoring + encoding.