Refactoring fill document function