Adding check for loops (fixes #27)