Fixed looping variable and added null safe behavior