Fixed loop and possible null check.