Big refactoring, part 1