Refactor some file operations