file handling refactored