Fixed infinite Loop in NodeWhen

And added some tests