Fix problem with leaving files opened