Improved file handling