Big code tidy up & impletemend PutDocument