rolled recursive marking into loops