refactor path insert function