Refactored file handling a bit.