Wade: add guard of "$val" to prevent infinite loop.