Fixed file IO code