Improved file reading code