Big refactoring for the core to make it less messy