added input argument to exist function so that only files are searched through, and not directories