Fixed file handling code