Clarify documentation and slightly reorder code