Add check for null before going into for..each

This overlaps a bit with #3 but adds more checks where a null can wreak havoc.