Correct various JS scope errors involving the variable in for...in loops