fixed path error and added check for existing files