Big refactoring of core