Better implementation of documentAttached.

Also some cleanup in composition.