Big cake-based refactor, largely for testability