Refactor filename handling (#7088)