Fixed file handle leaks