Massive refactor for the new API.