add conditions for debugging and loop check