Refactor code and separate globbing code into a function