Move duplicated code to base class
Close files and streams