add path filtering function, and tidy code