A lot of cleanups as well as minor API changes.