- added loop detection algorithm