Fixed file handles not getting properly disposed of