Protection against wrong loops

+removed unused variable