fixed missing valid check, fixed using the wrong path variable