backtracing

more compact code, skipping repeating elements easily