Fixed variable type mismatch in loop