improved file handling