Added check for loops. (fixes crash)