Refactor getFilename function