Refactored file operation code