Some more refactoring. Still wip.