Fixed another exception when path doesn't exist. Refactored into a single function