Refactored file handling.