Fix and reorganize filename processing