Cleanup, add comments, make some functions inline.
Match function signatures in front-facing stream API.