Some refactoring (still incomplete)