Code: Refactored WriteFile method.