simplify code to make it more readble, exit with error if path is NULL.