Fix serious bugs in file management