refactored script/doc saving