added handling of closed files.
fixed some errors