Refactor and add new function for recognizing existing file