Fixed inefficient loop