added path-checking code (laggy)