Main refactor, part 1