Refactor the file checks and directory expansions