Some work on the document API and minor style improvements.