Adding file functions and minor generalization