add option to wrap as whole document
Some optimizations