some bugfix. added depth for loop avoidance